Free AI web copilot to create summaries, insights and extended knowledge, download it at here
2829
Abstract
ref passed will be assigned to the element — <code>ref1</code> will be undefined.</p><figure id="c510"><img src="https://cdn-images-1.readmedium.com/v2/resize:fit:800/1*FD35zXsiUIEvY4DXHggkWg.png"><figcaption></figcaption></figure><p id="b3b6">So, here’s my <code>mergeRefs</code> function to merge these two refs, as if they’re one, and make them both functional. It copies the functional ref’s content to the undefined one so that both refs can be used for different purposes.</p>
<figure id="e886">
<div>
<div>
<iframe class="gist-iframe" src="/gist/XenoverseUp/6e37aaec9a8a0143aa2c19b52330b739.js" allowfullscreen="" frameborder="0" height="undefined" width="undefined">
</div>
</div>
</figure></iframe></div></div></figure><p id="1152">Once you have this function, you can assign as many refs as you want to a single component, without any flaw.</p><figure id="7a26"><img src="https://cdn-images-1.readmedium.com/v2/resize:fit:800/1*XUH84pG1D4lk4PwEAnnRkQ.png"><figcaption>`mergeRefs` function lets you assign multiple refs to a single element.</figcaption></figure><h1 id="5186">2. Private Routes To Handle Authentication Elegantly</h1><p id="05a4">Authentication is an integral part of a functional app or website. However, it, generally for beginners, looks scary and confusing. Especially when user roles come into play. Handling it correctly prevents many problems and complexity, later on.</p><p id="158f">In this trick, I’ll show my private route component. It creates a route that is only accessible by authenticated users that has appropriate roles. If the user doesn't suffice these conditions, it’ll be redirected to authentication or home page based on the authentication state.</p><p id="0c54">It receives the component of the route, roles that’ll have access to the route, and the other props passed. Then it returns a route that renders the private route conditionally.</p>
<figure id="a4ca">
<div>
<div>
<iframe class="gist-iframe" src="/gist/XenoverseUp/a7add9f02772633e95c0b34428a8a799.js" allowfullscreen="" frameborder="0" height="undefined" width="undefined">
</div>
</div>
</figure></iframe></div></div></figure><p id="72b5">Now that you have this private route component, you can use it just as a normal route in the router.</p><figure id="6824"><img src="https://cdn-images-1.readmedium.com/v2/resize:fit:800/1*zdsYFqPDvauyohvFyhh2XA.png"><figcaption>You can use these custom route components mixed with normal route components.</figcaption></figure><p id="b75d">The use case of this component is not limited to authentication purposes. You can use it basically anywhere that includes conditional l
Options
ogic. To see this component in action, check <a href="https://github.com/XenoverseUp/console-blog">this repository</a> out.</p><h1 id="d651">3. 15 Context Providers At The Same Time (!)</h1><p id="1f28">React contexts are useful. They help us pass data to child components without dealing with props from one component to another. Thanks to them, the data needed is available across the whole app. However, because of the comfort they provide, it is convenient to see this kind of things in the source code:</p>
<figure id="5110">
<div>
<div>
<iframe class="gist-iframe" src="/gist/XenoverseUp/e5de1c6894648a0e70b24dec204ec1f5.js" allowfullscreen="" frameborder="0" height="undefined" width="undefined">
</div>
</div>
</figure></iframe></div></div></figure><p id="0463">However, this is certainly not React style. They all have the same purpose, which is providing data but they’re written over and over. Just think of the unbearable headache of your developer friend, trying to figure out what this provider stack does.</p><p id="217f">So, let’s rewrite it in React style and make the code way more readable. To achieve this, we have to create a component named <code>Provider</code> that will encapsulate all context providers and act as one centralized provider.</p>
<figure id="8224">
<div>
<div>
<iframe class="gist-iframe" src="/gist/XenoverseUp/139a1c6eef4d29af82903e1f309b17ba.js" allowfullscreen="" frameborder="0" height="undefined" width="undefined">
</div>
</div>
</figure></iframe></div></div></figure><p id="c459">Then, we can simplify the root file by using the <code>Provider</code> component.</p>
<figure id="9470">
<div>
<div>
<iframe class="gist-iframe" src="/gist/XenoverseUp/e3ca7f5dd12447e5e03dc8733daa3bee.js" allowfullscreen="" frameborder="0" height="undefined" width="undefined">
</div>
</div>
</figure></iframe></div></div></figure><h1 id="3d14">Conclusion</h1><p id="3bb6">Congratulations! Now you know 3 more React tricks to ease and power up your development process. These were my observations and experiences. But I’m sure you have your own tricks, as well. Make sure you share it with me and others in the response section.</p><figure id="b261"><img src="https://cdn-images-1.readmedium.com/v2/resize:fit:800/1*TtX3EIdGpUqGCOW6LtO42Q.png"><figcaption>Subscribe to get your <b>free subscriber-exclusive</b> story.</figcaption></figure><p id="e9b8">If you liked this article, make sure you clapped and if you have anything to say, leave a response. See you in the next story.</p></article></body>