Ivan Kaminskyi

Aug 05, 20248 min

Render Props pattern in React

The Render Props pattern is a technique used in React to share code between components using a prop whose value is a function. This pattern allows you to pass a function as a prop to a component and render the result of that function inside the component's render method. Using the Render Props pattern, you can create reusable components encapsulating logic and share it with other elements. This article will discuss the basics of the Render Props pattern, how to use it in React, advanced usage, and common pitfalls to avoid.

Basic usage

The Render Props pattern is a technique used in React to share code between components using a prop whose value is a function. This pattern allows you to pass a function as a prop to an element and render the result of that function inside the component's render method. Using the Render Props pattern, you can create reusable components encapsulating logic and share it with other elements.

Here is an example of using the Render Props pattern in React:

import React from 'react';

import React, { useState, useEffect } from 'react';

const Mouse = ({ render }) => {
    const [position, setPosition] = useState({ x: 0, y: 0 });

    const handleMouseMove = (event) => {
        setPosition({
            x: event.clientX,
            y: event.clientY,
        });
    };

    useEffect(() => {
        const handleMouseMoveWrapper = (event) => handleMouseMove(event);
        window.addEventListener('mousemove', handleMouseMoveWrapper);
        return () => {
            window.removeEventListener('mousemove', handleMouseMoveWrapper);
        };
    }, []);

    return (
        <div style={{ height: '100vh' }}>
            {render(position)}
        </div>
    );
};

export default Mouse;

class App extends React.Component {
    render() {
        return (
            <div>
                <h1>Mouse Position</h1>
                <Mouse
                    render={({ x, y }) => (
                        <p>
                            X: {x}, Y: {y}
                        </p>
                    )}
                />
            </div>
        );
    }
}

export default App;

In the example above, we have a Mouse component that takes a render prop, a function that returns the JSX to render. The Mouse component uses the useState and useEffect hooks to track the mouse position and update the state accordingly. The render function is called with the current mouse position, and the result is rendered inside the Mouse component.

The App component renders the Mouse component and passes a function to the render prop that displays the current mouse position. This allows the Mouse component to encapsulate the logic for tracking the mouse position, while the App component can control how the mouse position is displayed.


Advanced usage

The Render Props pattern can be used to share more complex logic between components. For example, you can create a Toggle component that encapsulates the logic for toggling a state value and passing the state value and toggle function to other components using the Render Props pattern.

Here is an example of using the Render Props pattern to create a Toggle component:


import React, { useState } from 'react';

const Toggle = ({ children }) => {
    const [isToggled, setIsToggled] = useState(false);

    const toggle = () => {
        setIsToggled((prevIsToggled) => !prevIsToggled);
    };

    return children({ isToggled, toggle });
};

export default Toggle;

class App extends React.Component {
    render() {
        return (
            <div>
                <h1>Toggle Example</h1>
                <Toggle>
                    {({ isToggled, toggle }) => (
                        <div>
                            <button onClick={toggle}>Toggle</button>
                            {isToggled && <p>Toggle is on</p>}
                        </div>
                    )}
                </Toggle>
            </div>
        );
    }
}

export default App;

In the example above, we have a Toggle component that takes a children prop, a function that returns the JSX to render. The Toggle component uses the useState hook to track the toggle state and updates the state accordingly. The children function is called with the current toggle state and the toggle function, and the result is rendered inside the Toggle component.

The App component renders the Toggle component and passes a function to the children prop that displays a button to toggle the state value and a message based on the state value. This approach allows the Toggle component to encapsulate the logic for toggling a state value, while the App component can control how the toggle state is displayed.

Using the Render Props pattern, you can create reusable components that encapsulate logic and share it with other components flexibly and composable.


Render Props vs. Higher-Order Components (HOCs)

The Render Props pattern is an alternative to using Higher-Order Components (HOCs) in React. Both patterns allow you to share code between components but have different trade-offs.

The Render Props pattern is more flexible and composable, allowing you to pass any function as a prop to a component. This makes sharing complex logic between components and creating more reusable components easier. On the other hand, HOCs are more concise and can be easier to use in some cases, as they wrap a component with additional functionality. When deciding between the Render Props pattern and HOCs, consider the complexity of the logic you want to share and the flexibility you need in your components.

Code examples:

// Render Props pattern
const Mouse = ({ render }) => {
    const [position, setPosition] = useState({ x: 0, y: 0 });

    const handleMouseMove = (event) => {
        setPosition({
            x: event.clientX,
            y: event.clientY,
        });
    };

    useEffect(() => {
        const handleMouseMoveWrapper = (event) => handleMouseMove(event);
        window.addEventListener('mousemove', handleMouseMoveWrapper);
        return () => {
            window.removeEventListener('mousemove', handleMouseMoveWrapper);
        };
    }, []);

    return <div style={{ height: '100vh' }}>{render(position)}</div>;
};

// Higher-Order Component
const withMouse = (Component) => {
    return (props) => {
        const [position, setPosition] = useState({ x: 0, y: 0 });

        const handleMouseMove = (event) => {
            setPosition({
                x: event.clientX,
                y: event.clientY,
            });
        };

        useEffect(() => {
            window.addEventListener('mousemove', handleMouseMove);
            return () => {
                window.removeEventListener('mousemove', handleMouseMove);
            };
        }, []);

        return <Component {...props} mouse={position} />;
    };
};

export default withMouse;

const MouseWithHOC = withMouse(({ mouse }) => (
    <div style={{ height: '100vh' }}>
        <p>
            X: {mouse.x}, Y: {mouse.y}
        </p>
    </div>
));

In the example above, we have a Mouse component using the Render Props pattern and a MouseWithHOC component using a Higher-Order Component. Both components track the mouse position and render the current position, but they use different patterns to share the logic.

The Mouse component takes a render prop that returns the JSX to render, while the MouseWithHOC component uses a Higher-Order Component to pass the mouse position as a prop to the wrapped component. The Render Props pattern allows you to pass any function as a prop to the Mouse component, while the Higher-Order Component wraps the MouseWithHOC component with additional functionality.

When deciding between the Render Props pattern and Higher-Order Components, consider the complexity of the logic you want to share and the flexibility you need in your components.

The Render Props pattern is a powerful technique for creating reusable components in React and is worth considering when designing your component architecture.


Common pitfalls

When using the Render Props pattern in React, there are some common pitfalls to be aware of:

  1. Prop drilling: If you have multiple levels of components using the Render Props pattern, you may encounter prop drilling, where you need to pass the render function down through multiple levels of components. You can use context or custom hooks to share the render function across components to avoid prop drilling.
  2. Naming conflicts: When using the Render Props pattern, be mindful of naming conflicts between the render function and other props in your components. To avoid naming conflicts, use descriptive prop names and consider prefixing render prop names with render or children.
  3. Component re-renders: If the render function is passed to a component using the Render Props pattern, which creates new function instances on each render, it can cause unnecessary re-renders of the component. To avoid unnecessary re-renders, memoize the render function using useMemo or useCallback.
  4. Complexity: The Render Props pattern can introduce complexity to your components, especially when sharing complex logic between components. To manage complexity, break down logic into smaller components and use custom hooks to encapsulate shared logic.
  5. Performance: The Render Props pattern can impact performance if the render function creates many components or has expensive computations. To improve performance, optimize the render function and use memoization and lazy loading techniques.
  6. Code organization: When using the Render Props pattern, consider organizing your code to make it more maintainable and reusable. Use folder structures, naming conventions, and code comments to improve code readability and maintainability.
  7. Component composition: When composing components using the Render Props pattern, remember how components interact and share data. Use context or custom hooks to manage shared state and avoid prop drilling or naming conflicts.

By knowing these common pitfalls and following best practices, you can effectively use the Render Props pattern in your React applications and create more reusable and maintainable components.


Conclusion

The Render Props pattern is a powerful technique for sharing code between components in React. By passing a function as a prop to a component, you can create reusable components that encapsulate logic and share it with others. The Render Props pattern is flexible and composable and allows you to make more reusable components compared to Higher-Order Components (HOCs). When using the Render Props pattern, consider pitfalls like prop drilling, naming conflicts, component re-renders, complexity, performance, code organization, and component composition. By following best practices and avoiding common pitfalls, you can use the Render Props pattern effectively in your React applications and create more maintainable and reusable components.

Tags:
React
Share:

Get The Latest Insights straight To Your Inbox For Free!

Subscribe To The NewsletterSubscribe To The NewsletterSubscribe To The Newsletter