<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0" xmlns:cc="http://cyber.law.harvard.edu/rss/creativeCommonsRssModule.html">
    <channel>
        <title><![CDATA[Stories by Clara Dopico on Medium]]></title>
        <description><![CDATA[Stories by Clara Dopico on Medium]]></description>
        <link>https://medium.com/@clara.dopico?source=rss-35202f6936ea------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/1*_13Mr30jZmvjZdrtkug8SA.png</url>
            <title>Stories by Clara Dopico on Medium</title>
            <link>https://medium.com/@clara.dopico?source=rss-35202f6936ea------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Thu, 19 Nov 2026 18:12:24 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@clara.dopico/feed" rel="self" type="application/rss+xml"/>
        <webMaster><![CDATA[yourfriends@medium.com]]></webMaster>
        <atom:link href="http://medium.superfeedr.com/" rel="hub"/>
        <item>
            <title><![CDATA[The Problem with react-baidu-maps and Its Proptypes]]></title>
            <link>https://medium.com/trabe/the-problem-with-react-baidu-maps-and-its-proptypes-8122cb16a8ba?source=rss-35202f6936ea------2</link>
            <guid isPermaLink="false">https://medium.com/p/8122cb16a8ba</guid>
            <category><![CDATA[react]]></category>
            <category><![CDATA[baidu-maps]]></category>
            <category><![CDATA[react-baidu-maps]]></category>
            <category><![CDATA[prop-types]]></category>
            <dc:creator><![CDATA[Clara Dopico]]></dc:creator>
            <pubDate>Mon, 04 May 2020 08:01:01 GMT</pubDate>
            <atom:updated>2020-05-04T10:39:48.037Z</atom:updated>
            <content:encoded><![CDATA[<h3>The Problem with react-baidu-maps and the Abuse of PropTypes</h3><h4>How using cloneElement and magic propTypes can prevent a component from being really composable.</h4><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*IXkx6jjeimh2Yhi2" /><figcaption>Photo by <a href="https://unsplash.com/@hansonluu?utm_source=medium&amp;utm_medium=referral">Hanson Lu</a> on <a href="https://unsplash.com/?utm_source=medium&amp;utm_medium=referral">Unsplash</a></figcaption></figure><p><a href="https://medium.com/trabe/how-to-add-baidu-maps-to-your-application-without-learning-chinese-797b095ffcc8">In my last post</a>, we learned how to render some <a href="https://map.baidu.com/">Baidu Maps</a> in a simple way to avoid the Google Maps block in China. We reviewed and tried <a href="https://github.com/terence55/react-baidu-maps">react-baidu-maps</a>, a React library to render Baidu Maps. In those examples, we used the components exported by the library, like BaiduMap or Marker.</p><p>One of our projects is a React based framework that provides different components, including maps. Instead of exposing the react-baidu-maps components, we opted to wrap them and provide an abstraction to the user. This way we encapsulate the library internals controlling which props we want to expose to the user. We can even set some defaultProps to ensure a map will always be rendered even if some props are missing.</p><h4>Our component library</h4><p>Our custom Baidu Map implementation is defined in the following snippet. We wanted to control which props were available and their default values:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/a857b092c0cfe88b93d255ea63e0937e/href">https://medium.com/media/a857b092c0cfe88b93d255ea63e0937e/href</a></iframe><p>As it is defined in the PropTypes, only the Baidu Maps API Key prop is required. A line like &lt;BaiduMap apiKey=&quot;MY_API_KEY&quot; /&gt; will be enough to render a Baidu Map.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*oHMZObsM7fScTxg5Egz15Q.png" /><figcaption>A Baidu Map using our default props</figcaption></figure><p>Once we achieved this we were drawn to do the same with the other components we wanted to expose. For example, our own marker component:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/91b814c136476387498a6b2e9310b614/href">https://medium.com/media/91b814c136476387498a6b2e9310b614/href</a></iframe><p>We exposed some props and set some default values as well. But when we wanted to try out our map… Oh no! The map was not rendering and we found some errors on the console.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*FfvnCDUgybkpk7CBTNjEcA.png" /><figcaption>Chrome DevTools console error</figcaption></figure><h4>The problem with react-baidu-maps</h4><p>We faced the same problem when we tried to wrap other react-baidu-maps components like InfoWindow or MarkerClusterer. As it is shown in the console, the error is thrown in the library Marker component code. So the logical next move was to review the file to understand what that Cannot read property &#39;addOverlay&#39; of undefined meant.</p><p>Debugging the code we found that the error was thrown in the this.props[MAP].addOverlay(this.marker); line in thecomponentDidMount method:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/569fa9b7ded378e35c61ea7248583136/href">https://medium.com/media/569fa9b7ded378e35c61ea7248583136/href</a></iframe><p>As we can see in the snippet above, Marker has two propTypes objects [MAP] and [MARKER_CLUSTERER], two named constants imported from “<a href="https://github.com/terence55/react-baidu-maps/blob/master/src/utils/constants.js">utils/constants.js</a>”:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/615a8ce50bb7866451344c55050a30d2/href">https://medium.com/media/615a8ce50bb7866451344c55050a30d2/href</a></iframe><p>So…the secret is out. Some overlay components like InfoWindow or Marker must have props with these funny names. Searching for the uses of these constants in the library, we found the BaiduMap component:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/9973b6d981b58f7e7857cd26073d8209/href">https://medium.com/media/9973b6d981b58f7e7857cd26073d8209/href</a></iframe><p>The renderChildren method checks if each of the Map children contains a [MAP] propType (also known as SECRET_MAP_DO_NOT_USE_OR_YOU_WILL_BE_FIRED). In that case, it injects a map element in the mentioned SECRET_MAP_DO_NOT_USE_OR_YOU_WILL_BE_FIRED prop. This checking is necessary to ensure that a compatible element, like a Marker or InfoWindow, is being cloned with React cloneElement. Here is where our problem originated: our custom components did not have a propType with that name.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/cabcc66c93de5a89042b13a1e3d9d299/href">https://medium.com/media/cabcc66c93de5a89042b13a1e3d9d299/href</a></iframe><p>In the Marker case, if no prop called SECRET_MAP_DO_NOT_USE_OR_YOU_WILL_BE_FIRED is received that prop will be undefined. Thus, the previously mentioned line this.props[MAP].addOverlay(this.marker) in the componentDidMount method will throw an error.</p><h4>A solution</h4><p>In an ideal world, the react-baidu-maps would use the <a href="https://reactjs.org/docs/context.html">React Context API</a> instead of using cloneElement and filtering the children using magic propTypes. Sadly, it is not the case, so we have to play by the library rules and adapt our code to this.</p><p>We need to add the required propTypes to our custom components. We can create our own “utils/constants.js” to hide the funny naming, or directly add the constants like in the following examples:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/483dfb357304c4fa60324996fee717ab/href">https://medium.com/media/483dfb357304c4fa60324996fee717ab/href</a></iframe><p>As we can see in the <a href="https://github.com/terence55/react-baidu-maps/blob/master/src/overlays/InfoWindow.js">react-baidu-maps </a><a href="https://github.com/terence55/react-baidu-maps/blob/master/src/overlays/InfoWindow.js">InfoWindow source code</a>, this overlay component expects two props [MAP] and [MARKER], so we need to add the corresponding two propTypes:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/996fd6bc63eddbf61122ecc98da31103/href">https://medium.com/media/996fd6bc63eddbf61122ecc98da31103/href</a></iframe><p>Once we export our custom components, we can simply build an example like the following one:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/a6f3b142f1d7c97889716a7fbee18170/href">https://medium.com/media/a6f3b142f1d7c97889716a7fbee18170/href</a></iframe><p>This way we can successfully render a Baidu Map without any errors:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*-GT3F8Gl_OByMdBI0rHxFw.png" /><figcaption>A Baidu Map with a Marker and an InfoWindow</figcaption></figure><h4>Wrapping up</h4><p><a href="https://github.com/terence55/react-baidu-maps">react-baidu-maps</a> is a library designed to be used through its exported components. In some cases, you might need to override its behavior or you want to hide the library internals from the user. Due to the library internals, based on filtering the children using magic propTypes and cloning elements, you will need to add some specifics propTypes to your custom components. This is a messy approach but it will avoid some errors, headaches and you will get your Baidu Maps running. There are other APIs, like the Context API, that let components pass data to deeply nested descendants, that will adapt better to this scenario and are composable-friendly solutions.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=8122cb16a8ba" width="1" height="1" alt=""><hr><p><a href="https://medium.com/trabe/the-problem-with-react-baidu-maps-and-its-proptypes-8122cb16a8ba">The Problem with react-baidu-maps and Its Proptypes</a> was originally published in <a href="https://medium.com/trabe">Trabe</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[How to add Baidu Maps to your application without learning Chinese]]></title>
            <link>https://medium.com/trabe/how-to-add-baidu-maps-to-your-application-without-learning-chinese-797b095ffcc8?source=rss-35202f6936ea------2</link>
            <guid isPermaLink="false">https://medium.com/p/797b095ffcc8</guid>
            <category><![CDATA[baidu-maps]]></category>
            <category><![CDATA[baidu]]></category>
            <category><![CDATA[react-baidu-maps]]></category>
            <category><![CDATA[react]]></category>
            <dc:creator><![CDATA[Clara Dopico]]></dc:creator>
            <pubDate>Mon, 20 Apr 2020 08:01:01 GMT</pubDate>
            <atom:updated>2020-04-20T08:01:01.537Z</atom:updated>
            <content:encoded><![CDATA[<h3>How to Add Baidu Maps to Your Application Without Learning Chinese</h3><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*1n6GMkN_UjZSVM7X" /><figcaption>Photo by <a href="https://unsplash.com/@chuttersnap?utm_source=medium&amp;utm_medium=referral">chuttersnap</a> on <a href="https://unsplash.com/?utm_source=medium&amp;utm_medium=referral">Unsplash</a></figcaption></figure><p>Google Maps, like all the Google services, is blocked in the People’s Republic of China. Due to their <a href="https://en.wikipedia.org/wiki/Internet_censorship_in_the_People%27s_Republic_of_China">Internet censorship policy</a>, certain websites can only be accessed via VPN. We must accept it and use any of the Chinese map services alternatives, like <a href="https://map.baidu.com/">Baidu Maps</a> or <a href="https://www.amap.com/">Gaode Maps</a>.</p><p>We must keep this in mind not only when traveling to China but when we develop our applications too. In our case, we had to adapt our React applications to use a different map service when the user accesses them from the Asian country.</p><h3>Baidu Maps</h3><p><a href="https://map.baidu.com/">Baidu Maps</a> is the most used web map service in China. It offers features like satellite imagery, street maps, street view, and even indoor view but it is available only in the Chinese language.</p><p>To access Baidu APIs, you will need to <a href="https://passport.baidu.com/v2/?reg&amp;regType=1&amp;tpl=netdisk&amp;overseas=1">register as a developer</a> and create a valid <a href="http://lbsyun.baidu.com/apiconsole/key/create">API Key</a>. It used to be mandatory to have a Chinese phone number to register in <a href="https://passport.baidu.com/">Baidu</a>. Now you can also do it through this <a href="https://passport.baidu.com/v2/?reg&amp;overseas=1">link</a> for overseas registrations, although some countries, like the European ones, are not supported yet. You can overcome this by getting a compatible mobile phone number using a web service like <a href="https://www.skype.com/en/features/online-number/">Skype</a>.</p><p>In our case, we decided to use a third-party library. A library that encapsulates the Baidu API offering us a wrapped and simpler version of the API. Preferably in a known language. We found a couple of React libraries like <a href="https://github.com/terence55/react-baidu-maps">react-baidu-maps</a> or <a href="https://github.com/weflex/react-baidu-map">react-baidu-map</a>, but we dismissed the latter because its latest commit is from 2016.</p><h3>react-baidu-maps</h3><p><a href="https://github.com/terence55/react-baidu-maps">react-baidu-maps</a> exposes React components wrapping the Baidu Maps API. This library lets us render a Baidu map without having to access or manage the real Baidu API. Besides, its documentation is in English.</p><p>Its Github documentation is not too exhaustive, but the examples allow us to render a simple map example:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*Drigb30SQWIDYYfJywtHEA.png" /><figcaption>Baidu Maps documentation examples</figcaption></figure><p>As we see in the example, to use the BaiduMap component dynamically with asynchronous load, we need to use the asyncWrapper function to wrap the BaiduMap component.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*vOnV4AmBETRSXEzQY7UklA.png" /><figcaption>A simple Baidu Map</figcaption></figure><p>These examples are enough to render a simple map. But to fully grasp how this library works we recommend to follow the next steps:</p><ul><li>Read the <a href="https://github.com/terence55/react-baidu-maps">react-baidu-maps README.md</a>.</li><li>Download the repo or go over its Github page to examine the code.</li><li>Review the <a href="https://github.com/terence55/react-baidu-maps/blob/13e16f6fa72f8951d33b8441797122de39c4c67b/src/BaiduMap.js">BaiduMap props</a>, understanding its types and naming.</li><li>Experiment with the props to customize the map examples.</li><li>Repeat the process with every component from the library that we want to add to our maps.</li></ul><h4>Customizing a Baidu Map</h4><p>BaiduMap has some statically defined PropTypes within the component and other namedcontrolledPropTypes. Among the first ones, we find different props to define the map by default. We must notice that the mapContainer prop appears as required, so we must define a container for the map. The mapContainer or the element wrapping the map must have a specific height. The documentation does not explicitly specify it, but otherwise, we will only see a blank page.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/5d69b4a84f3051c35915625133e3c339/href">https://medium.com/media/5d69b4a84f3051c35915625133e3c339/href</a></iframe><p>The controlledPropTypes object includes props as:</p><ul><li>the map center.</li><li>the zoom level.</li><li>whether to allow different movements on the map.</li><li>the type of map (normal, perspective, satellite, hybrid).</li></ul><p>We must also review the shapes of each one of these props, defined in <a href="https://github.com/terence55/react-baidu-maps/blob/13e16f6fa72f8951d33b8441797122de39c4c67b/src/utils/MapPropTypes.js">src/utils/MapPropTypes</a>.</p><p>Applying some of the previously mentioned props, we can easily render a custom map:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/33cbc1d56348de25c1c469012578b890/href">https://medium.com/media/33cbc1d56348de25c1c469012578b890/href</a></iframe><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*8tWVY2xeFKDr_MCWuwSOng.png" /><figcaption>A customized Baidu Map</figcaption></figure><p>Moreover, the library offers us all Baidu Maps supported overlays. Like Marker, InfoWindow, Circle, Curve, etc.</p><p>We can complete our example by adding a Marker that shows an InfoWindow (an information popup) when it is clicked. We need to thoroughly review the <a href="https://github.com/terence55/react-baidu-maps/blob/13e16f6fa72f8951d33b8441797122de39c4c67b/src/overlays/Marker.js">Marker</a> and <a href="https://github.com/terence55/react-baidu-maps/blob/13e16f6fa72f8951d33b8441797122de39c4c67b/src/overlays/InfoWindow.js">InfoWindow</a> propTypes as well.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/931eb798d5bda6f83b74c118119a7ed2/href">https://medium.com/media/931eb798d5bda6f83b74c118119a7ed2/href</a></iframe><p>This simple code snippet renders a 400px height map centered in Beijing. Now we are able to drag the map or zoom in with our scroll wheel and by double-clicking.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*kf59Z8Fh21LfBOmel3CIjQ.png" /><figcaption>A customized Baidu Map with a marker in Beijing</figcaption></figure><p>With these simple steps, you will be able to render your own customized Baidu Maps. Now it is your turn to try it out. Good Luck!</p><h4>Summing up</h4><p>Due to the People’s Republic of China’s web restrictions, some of the most widely used internet services are blocked. For example, Google Maps, which forces us to adapt our worldwide targeted apps to this situation. Trying to use a local map service, like Baidu Maps, can be challenging. Its websites are only in Chinese which can be overwhelming if you don’t know the language. An option is using a third-party library, which encapsulates the Baidu API offering us a wrapped and simpler version. Using react-baidu-maps we can render maps without mastering the Baidu API or using a dictionary.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=797b095ffcc8" width="1" height="1" alt=""><hr><p><a href="https://medium.com/trabe/how-to-add-baidu-maps-to-your-application-without-learning-chinese-797b095ffcc8">How to add Baidu Maps to your application without learning Chinese</a> was originally published in <a href="https://medium.com/trabe">Trabe</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Getting rid of findDOMNode method in your React application]]></title>
            <link>https://medium.com/trabe/getting-rid-of-finddomnode-method-in-your-react-application-a0d7093b2660?source=rss-35202f6936ea------2</link>
            <guid isPermaLink="false">https://medium.com/p/a0d7093b2660</guid>
            <category><![CDATA[react]]></category>
            <category><![CDATA[ref]]></category>
            <category><![CDATA[finddomnode]]></category>
            <category><![CDATA[react-16]]></category>
            <category><![CDATA[react-dom]]></category>
            <dc:creator><![CDATA[Clara Dopico]]></dc:creator>
            <pubDate>Mon, 20 May 2019 08:01:00 GMT</pubDate>
            <atom:updated>2019-05-20T08:01:00.733Z</atom:updated>
            <content:encoded><![CDATA[<h3>Getting rid of findDOMNode in your React application</h3><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*Mwh0Y8OdGjOkHcmQ" /><figcaption>Photo by <a href="https://unsplash.com/@annietheby?utm_source=medium&amp;utm_medium=referral">Annie Theby</a> on <a href="https://unsplash.com/?utm_source=medium&amp;utm_medium=referral">Unsplash</a></figcaption></figure><p>Lately, our team has been migrating a project from React 16.2 to the latest version, <a href="https://reactjs.org/blog/2019/02/06/react-v16.8.0.html">16.8</a>. One of the challenges that we have encountered during this migration phase is the deprecation of <a href="https://reactjs.org/docs/react-dom.html#finddomnode">findDOMNod</a>e. This <a href="https://reactjs.org/docs/react-dom.html">ReactDOM</a> method allows us to access the underlying DOM node of a component.</p><p>Using React <a href="https://reactjs.org/docs/strict-mode.html">StrictMode</a> to check potential problems in our application, we ran into log warnings about our findDOMNode usages:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/800/1*5sg5GGuogaaqzLYIl2B2WQ.png" /><figcaption>StrictMode warning messages</figcaption></figure><p>As we can see, findDOMNode is deprecated in StrictMode. Its usage is discouraged and, possibly, it will be deleted in a future version. The warning message recommends us to add a <a href="https://reactjs.org/docs/refs-and-the-dom.html">ref</a> directly to the element we want to reference. But sometimes, when you are migrating a complex application, the solution may not be that simple. In some cases, for example, your components are formed only by other components, there is no DOM element (such as div or span) where to attach the ref.</p><p>Using findDOMNode this was not a problem. You could pass directly this or add a ref to any React component to retrieve the corresponding DOM element:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/4d4c9f0924a9521ad458f991d3f8a05e/href">https://medium.com/media/4d4c9f0924a9521ad458f991d3f8a05e/href</a></iframe><p>So let’s go over the different solutions we have come across while trying to get rid of our findDOMNode usages.</p><h3>Adding a DOM node wrapper</h3><p>The simplest solution to this type of scenario is to wrap your component with a DOM element that you can actually attach a ref to it. For example:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/9fd30c73aaa16af19f0a6c5b3d1fc1a6/href">https://medium.com/media/9fd30c73aaa16af19f0a6c5b3d1fc1a6/href</a></iframe><p>The drawback of this solution is that it can get very tricky when you’re migrating a large application. For example, having many nested components positioned relative to each other can lead you to a CSS nightmare.</p><h3>Using ref forwarding</h3><p>Another solution that the <a href="https://reactjs.org/docs/strict-mode.html#warning-about-deprecated-finddomnode-usage">React documentation</a> recommends us is to pass a ref to your custom component and pass that along to the DOM using <a href="https://reactjs.org/docs/forwarding-refs.html#forwarding-refs-to-dom-components">ref forwarding</a>. This way it is not necessary to wrap our components or add new HTML elements.</p><p>Applied to our previous example:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/a3074489524b635c43997e49739cf6e7/href">https://medium.com/media/a3074489524b635c43997e49739cf6e7/href</a></iframe><p>The problem we can encounter using this approach is that it can lead us to the problem of the broken ref. As my colleague <a href="https://medium.com/u/85c2933da91c">Ceci García García</a> explained in her post, <a href="https://medium.com/trabe/a-better-way-of-using-refs-in-react-dd970d2b13e5">A better way of using refs in React</a>, in case our component needs to be wrapped by other custom components or third-party libraries the ref will be lost. We will not be able to access that value anymore.</p><p>A typical case in our application that led us to a broken ref situation was how to position components like dropdowns or tooltips. That kind of component renders in a different layer in a determined location. In those cases, we need to retrieve the DOM node to get information like the element size or relative position to the viewport to locate them. We solved this by creating some <a href="https://reactjs.org/docs/higher-order-components.html">HOCs</a> that inject the size or the position to the components. One of them is the following example, used to inject the offsetParent to a component:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/e367545d5ef5d93235272b4c358b3a7f/href">https://medium.com/media/e367545d5ef5d93235272b4c358b3a7f/href</a></iframe><p>As we can see, this HOC wraps the component injecting its offsetParent as prop, a value that it obtains through findDOMNode(this). We can’t just replace it with a ref attached to WrappedComponent. We can’t wrap everything with a div or any other DOM element because it will lead us to some serious CSS drama. We shouldn’t use the forwardRef solution either, because it would not allow us to compose several HOCs or wrap the component with other components. So, how did we solve this?</p><h3>Explicitly pass the ref as a prop to your component</h3><p>The solution that we finally chose is to create a ref in the HOC but passing it through its wrapped component via props (domNodeRef in the example). Then, the final component will have the responsibility to attach it to an actual DOM element.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/362562bf9f87b2ccac95fb98b9d65166/href">https://medium.com/media/362562bf9f87b2ccac95fb98b9d65166/href</a></iframe><p>For example, injectOffset HOC is used by the Dropdown component. It receives as props both the offsetParent and the domNodeRef and attaches the latter to the corresponding div element:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/cc5dc1e41ea7365f72b4d012f9771d04/href">https://medium.com/media/cc5dc1e41ea7365f72b4d012f9771d04/href</a></iframe><p>The disadvantage of this method is that components that use our HOCs will need to know what to do with the domNodeRef they are receiving. But this solution allows us to wrap these components or use third-party libraries without fear of losing our ref.</p><h3>Summing up</h3><p>React findDOMNode method is already deprecated in StrictMode. It’s important to get rid of all its usages in our code to be ready to migrate to future React versions. The simplest solution is to replace it with a ref attached to the element we are interested in referencing (or a wrapper of it). But if we have to migrate an extensive and complex application this may not work for us in all cases. In those scenarios solutions like using ref forwarding or explicitly passing the ref as a prop can save us some tears.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=a0d7093b2660" width="1" height="1" alt=""><hr><p><a href="https://medium.com/trabe/getting-rid-of-finddomnode-method-in-your-react-application-a0d7093b2660">Getting rid of findDOMNode method in your React application</a> was originally published in <a href="https://medium.com/trabe">Trabe</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
    </channel>
</rss>