DEV Community

John Liu
John Liu

Posted on

2 1

React Render Props

When you are building your pet React project, sometimes you may want to reuse a component across other components to follow the DRY principle. This is where the render props pattern may shine.


According to the React documentation, render props is "a technique for sharing code between React components using a prop whose value is a function."

Let's use an example to break down this definition. We have a simple React application that will display two sassy comments depending on the random number that is provided to it. If the random number is greater than zero, then display "Whoa, Big Guy!". If random number is less than or equal to zero, then display "Nah, less than one, Nice Try!".

function App() {
    return (
        <div>
            <RandomNumber render={
                function(number) {
                    return (
                        <h1>{number > 0 ? "Whoa, Big Guy!" : "Nah, less than one, Nice Try!"}</h1>
                    )
                }
            }/>
        </div>
    )
}


function RandomNumber(props) { 
    const max = Math.random()
    const min = Math.random()
    const number = max - min
    return (
        <div>
            {props.render(number)}
        </div>
    )
}

ReactDOM.render(<App />,document.getElementById('root'))
Enter fullscreen mode Exit fullscreen mode

If you want to play along, you can access the code in codepen.

Here we see that we have one component <App /> that contains a child component <RandomNumber />.

  • The child component <RandomNumber /> will manage the state of the random number, but it will not care whether <App /> is rendering the display of it or another parent component.

  • The parent component <App /> will manage the display of the random number and the corresponding message. It will however not maintain the state of the random number.

Now, what if we have to share the <RandomNumber /> component across three parent components? Let's see what it looks like below.

function App() {
    return (
      <>
        <ParentOne />
        <ParentTwo />
        <ParentThree />
      </>
    )
}

function ParentOne() {
    return (
        <div>
            <RandomNumber render={
                function(number) {
                    return (
                        <h1>{number > 0 ? "Whoa, Big Guy!" : "Nah, less than one, Nice Try!"}</h1>
                    )
                }
            }/>
        </div>
    )
}

function ParentTwo() {
    return (
        <div>
            <RandomNumber render={
                function(number) {
                    return (
                        <h1>{number > 0 ? "Whoa, Big Guy!" : "Nah, less than one, Nice Try!"}</h1>
                    )
                }
            }/>
        </div>
    )
}

function ParentThree() {
    return (
        <div>
            <RandomNumber render={
                function(number) {
                    return (
                        <h1>{number > 0 ? "Whoa, Big Guy!" : "Nah, less than one, Nice Try!"}</h1>
                    )
                }
            }/>
        </div>
    )
}

function RandomNumber(props) { 
    const max = Math.random()
    const min = Math.random()
    const number = max - min
    return (
        <div>
            {props.render(number)}
        </div>
    )
}

ReactDOM.render(<App />,document.getElementById('root'))
Enter fullscreen mode Exit fullscreen mode

If you want to play along, you can access the code in codepen.

This is essentially the same code as the first one above. However, now we have three parents. Notice <RandomNumber /> child component did not have to change to accommodate each parent. It simply maintains a random number for each parent component. Each parent component, in turn, manages the rendering of the JSX separately and independent of each other.


Caveat: This is my personal notes on learning React. If you see any mistakes, would appreciate pointing it out. Thank you!

Date Created: December 16, 2021

Hostinger image

Get n8n VPS hosting 3x cheaper than a cloud solution

Get fast, easy, secure n8n VPS hosting from $4.99/mo at Hostinger. Automate any workflow using a pre-installed n8n application and no-code customization.

Start now

Top comments (0)

AWS Q Developer image

Your AI Code Assistant

Automate your code reviews. Catch bugs before your coworkers. Fix security issues in your code. Built to handle large projects, Amazon Q Developer works alongside you from idea to production code.

Get started free in your IDE

👋 Kindness is contagious

If this post resonated with you, feel free to hit ❤️ or leave a quick comment to share your thoughts!

Okay