
Security News
curl Shuts Down Bug Bounty Program After Flood of AI Slop Reports
A surge of AI-generated vulnerability reports has pushed open source maintainers to rethink bug bounties and tighten security disclosure processes.
react-composer
Advanced tools
Compose render prop components.
Render props are great. Using a component with a render prop looks like the following:
<RenderPropComponent {...config}>
{result => <MyComponent result={result} />}
</RenderPropComponent>
Sometimes you need the result of multiple render prop components inside of MyComponent. This
can get messy.
<RenderPropComponent {...config}>
{resultOne => (
<RenderPropComponent {...configTwo}>
{resultTwo => (
<RenderPropComponent {...configThree}>
{resultThree => (
<MyComponent results={{ resultOne, resultTwo, resultThree }} />
)}
</RenderPropComponent>
)}
</RenderPropComponent>
)}
</RenderPropComponent>
Nesting render prop components leads to rightward drift of your code. Use React Composer to prevent that drift.
import Composer from 'react-composer';
<Composer
components={[
<RenderPropComponent {...configOne} />,
<RenderPropComponent {...configTwo} />,
<RenderPropComponent {...configThree} />
]}>
{([resultOne, resultTwo, resultThree]) => (
<MyComponent results={{ resultOne, resultTwo, resultThree }} />
)}
</Composer>;
Install using npm:
npm install react-composer
or yarn:
yarn add react-composer
This library has one, default export: Composer.
<Composer />Compose multiple render prop components together. The props are as follows:
props.childrenA render function that is called with an array of results accumulated from the render prop components.
<Composer components={[]}>
{results => {
/* Do something with results... Return a valid React element. */
}}
</Composer>
props.componentsThe render prop components to compose. This is an array of React elements and/or render functions that are invoked with a render function and the currently accumulated results.
<Composer
components={[
// React elements may be passed for basic use cases
// props.children will be provided via React.cloneElement
<Outer />,
// Render functions may be passed for added flexibility and control
({ results, render }) => (
<Middle previousResults={results} children={render} />
)
]}>
{([outerResult, middleResult]) => {
/* Do something with results... Return a valid React element. */
}}
</Composer>
Note: You do not need to provide
props.childrento the React element entries inprops.components. If you do provideprops.childrento these elements, it will be ignored and overwritten.
props.components as render functionsA render function may be passed instead of a React element for added flexibility.
Render functions provided must return a valid React element. Render functions will be invoked with an object containing 2 properties:
results: The currently accumulated results. You can use this for render prop components which depend on the results of other render prop components.render: The render function for the component to invoke with the value produced. Plug this into your render prop component. This will typically be plugged in as props.children or props.render.<Composer
components={[
// props.components may contain both elements and render functions
<Outer />,
({ /* results, */ render }) => <SomeComponent children={render} />
]}>
{results => {
/* Do something with results... */
}}
</Composer>
<Composer
components={[
<Outer />,
({ results: [outerResult], render }) => (
<Middle fromOuter={outerResult} children={render} />
),
({ results, render }) => (
<Inner fromOuterAndMiddle={results} children={render} />
)
// ...
]}>
{([outerResult, middleResult, innerResult]) => {
/* Do something with results... */
}}
</Composer>
props.children.By default, <Composer /> will enhance your React elements with props.children.
Render prop components typically use props.children or props.render as their render prop. Some even accept both. For cases when your render prop component's render prop is not props.children you can plug render in directly yourself. Example:
<Composer
components={[
// Support varying named render props
<RenderAsChildren />,
({ render }) => <RenderAsChildren children={render} />,
({ render }) => <RenderAsRender render={render} />,
({ render }) => <CustomRenderPropName renderItem={render} />
// ...
]}>
{results => {
/* Do something with results... */
}}
</Composer>
Example of how to handle cases when a component passes multiple arguments to its render prop rather than a single argument.
<Composer
components={[
<Outer />,
// Differing render prop signature (multi-arg producers)
({ render }) => (
<ProducesMultipleArgs>
{(one, two) => render([one, two])}
</ProducesMultipleArgs>
),
<Inner />
]}>
{([outerResult, [one, two], innerResult]) => {
/* Do something with results... */
}}
</Composer>
This library only works for render prop components that have a single render prop. So, for instance, this library will not work if your component has an API like the following:
<RenderPropComponent onSuccess={onSuccess} onError={onError} />
The first item in the components array will be the outermost component that is rendered. So, for instance,
if you pass
<Composer components={[<A/>, <B/>, <C/>]}>
then your tree will render like so:
- A
- B
- C
Render prop components often specify with PropTypes that the render prop is required. When using these components with React Composer, you may get a warning in the console.
One way to eliminate the warnings is to define the render prop as an empty function knowning that Composer will
overwrite it with the real render function.
<Composer
components={[
<RenderPropComponent {...props} children={() => null} />
]}
// ...
>
Alternatively, you can leverage the flexibility of the props.components as functions API and plug the render function in directly yourself.
<Composer
components={[
({render}) => <RenderPropComponent {...props} children={render} />
]}
// ...
>
Here are some examples of render prop components that benefit from React Composer:
Do you know of a component that you think benefits from React Composer? Open a Pull Request and add it to the list!
Are you interested in helping out with this project? That's awesome – thank you! Head on over to the contributing guide to get started.
Recompose is a React utility belt for function components and higher-order components. It provides a set of helper functions for creating higher-order components, which can be used to achieve similar composition patterns as `react-composer`. However, Recompose is more focused on higher-order components rather than render props.
React Adopt is another library for composing render props. It provides a `Adopt` component that allows you to combine multiple render prop components in a similar way to `react-composer`. The main difference is in the API and the way you define the composition.
React Contextual is a library for managing state with React's context API. While it is not specifically for composing render props, it can be used to achieve similar patterns by providing a way to manage and compose state across multiple components.
FAQs
Compose render prop components
The npm package react-composer receives a total of 474,833 weekly downloads. As such, react-composer popularity was classified as popular.
We found that react-composer demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Security News
A surge of AI-generated vulnerability reports has pushed open source maintainers to rethink bug bounties and tighten security disclosure processes.

Product
Scan results now load faster and remain consistent over time, with stable URLs and on-demand rescans for fresh security data.

Product
Socket's new Alert Details page is designed to surface more context, with a clearer layout, reachability dependency chains, and structured review.