Must Know React Portability Patterns
React, the popular JavaScript library for building user interfaces, offers several portability patterns that developers must be familiar with in order to create reusable and maintainable components. Here are some must-know React portability patterns:
1. Higher-Order Components (HOC)
Higher-Order Components are a powerful pattern in React used for code reuse, logic abstraction, and render prop manipulation. They are functions that take a component and return a new component with enhanced functionality. This pattern allows you to extract common logic from components and reuse it across different parts of your application.
2. Render Props
Render Props is another portability pattern in React that allows components to share code and state with other components by using a prop whose value is a function. This pattern is useful for creating components that can be used in different contexts and easily plugged into different parts of the application.
3. Hooks
Introduced in React 16.8, Hooks allow developers to reuse stateful logic between components without changing the component hierarchy. By using built-in hooks such as useState and useEffect, developers can extract and share logic across components, making code more modular and easier to maintain.
4. Context API
The Context API allows you to share data between components without having to pass props through every level of the component tree. This pattern is useful for creating global state or sharing common data and logic across different parts of the application.
5. Custom Hooks
Custom Hooks are a way to extract reusable logic from components and share it between multiple components. By creating custom hooks, developers can encapsulate common logic and state handling, making it easier to reuse and maintain across the application.
By understanding and leveraging these portability patterns, developers can create more flexible, reusable, and maintainable components in their React applications. Whether it’s using Higher-Order Components, Render Props, Hooks, Context API, or Custom Hooks, these patterns provide powerful tools for building modular and portable components in React.
To try everything Brilliant has to offer—free—for a full 30 days, visit http://brilliant.org/JackHerrington/ . The first 200 of you will get 20% off Brilliant’s annual premium subscription.
uses react
talks about fighting lockin
great content😉
This was awesome. Thanks for enlightening me. I made a Vite/Vue app as a passion project late last year. Now I know how to structure things on my next project or when I update it!
Hi Jack! Thank you for the content, you rock! Do you think that is a good practice to share code between different apps in a monorepo? Will that not make the changeability of the apps more difficult? In what cases it is recommended to share? Thanks!
These videos give me brain tingles 😀
One thing I would be interested in would be whether you could do this with a completely different framework, like vue or svelte instead of another type of react app.
Golden video that shows how to do dependency injection with react context like we have in angular apps. Saved the day
Great video and a nice pattern for people to learn, especially as all the big React frameworks get snapped up by large vendors that want to keep you on their platform.
Just curious, do you think this could be extended to work with Qwik and the qwik-react plugin?
Really awesome and informative learned a lot🔥. Prior to watching the video, I had some confusion about the differences between module federation and Monorepos (using Nx). However, this video provided much-needed clarity. my team is currently in the process of merging two Next.js projects – one of which utilizes Redux while the other utilizes Context API. I will definitely discuss this pattern with my team. btw any thoughts how we can go about this?
Jack, as a junior React/TS dev, I really appreciate your content. You are pretty much my go-to source of the aforementioned tech. Just want you to know how much you are helping us out in the community. Appreciate your effort.
14:50 I wonder whether this is another case of developer convenience imposing a runtime cost when the maintenance concern could be addressed with a build time solution.
The capabilities that a capacitor/web application encounters are not going to vary for the duration of the application session so it really doesn't seem like a runtime concern (i.e. a case for late binding).
Granted this is just a standard case of runtime dependency injection but maybe there is a better way; an environment module.
Configuring the build tooling to resolve to the correct implementing module (e.g. rollup/plugin-alias ) may be a bit more hassle but it would remove the runtime indirection of retrieving the component repeatedly from the context.
[PHP Frameworks Day 2013 [eng] / Rasmus Lerdorf; 31m49s]
Excellent video, really useful info.
Your background is really awesome is it real 😐
Would you suggest using Lerna or NX for monorepos?
Great video, thanks! If you add a React Native App, you would add the basic elements (View, Text, etc) to the Environment Context?
Make react or next js course 25-50 dollars
I'm happy, when Jack is happy!
I loved the video. I've used that technique of sharing React components through context but I always felt it was like an anti-pattern. Glad to see it wasn't such a bad idea.
Please create the series on React Router Dom v6
It's overwhelming