Lately, I’ve been leveraging the Render Props pattern in React across both work and side projects, and it’s been incredibly effective! This pattern is a powerful way to create reusable components and share logic. For instance, imagine a situation where a component on a page needs some dynamic or stateful data. If this state is managed at the page level, every update could trigger a full-page re-render—something we’d want to avoid. Render Props provide a clean solution by isolating state management within the component itself, keeping the rest of the page unaffected. Here’s a simple counter example: #react #reactjs #typescript #javascript
Wrote a small post about the Container Presentation Pattern in React here: https://www.linkedin.com/posts/ashish-kumar-aa5927b2_reactjs-frontend-reactnative-activity-7272628770849615872-FfXO?utm_source=share&utm_medium=member_android
Am curious, does it only work this way with the JSX getting generated by the arrow function or is it also possible to define the render props in Counter but the children can be nested inside Counter like: <Counter ...> <Children /> </Counter> ?
It's very good, and in some situations, I often use it. However, it can be quite challenging to maintain when working with a team. In certain cases, some persons use patterns like this in components with very complex logic, making it hard to read. The worst-case scenario is when multiple hooks are used inside this, leading to a heavy rendering process. Nevertheless, I think this is a very cool code pattern if it's not overused.😎 Thanks for sharing Ashish Kumar 🙏
Rough idea, but you could also do this with contextual elements. You get a bit more composability out of it this way.
I tend to use render props nowadays whenever I need to have a specific markup.
What about signals?
Yeah, Render Props pattern could be handy in some situations. However, personally, I'm not a big fan of it because it can lead to messy or unreadable code if not used carefully. To be honest, having logic inside markup doesn't look good to me.
But doesn't the component passed to the render prop re-render all the time, because you are effectively passing a new object each time ?
I prefer custom hooks.
Frontend Developer | React, JavaScript, TypeScript & Next.js | Scalable Web App Architect | UX/UI Innovator at Rakuten India
2wWrote some points about Compound components in React Here: https://www.linkedin.com/posts/ashish-kumar-aa5927b2_react-reactjs-typescript-activity-7269916064971399171-0bkr?utm_source=share&utm_medium=member_android