Compose render prop components
npm install react-util-tea-x




Compose render prop components.
Render props are great. Using a component with a render prop looks like the following:
``jsx`
{result =>
Sometimes you need the result of multiple render prop components inside of MyComponent. This
can get messy.
`jsx`
{resultOne => (
{resultTwo => (
{resultThree => (
)}
)}
)}
Nesting render prop components leads to rightward drift of your code. Use React Composer to
prevent that drift.
`jsx
import Composer from 'react-composer';
]}>
{([resultOne, resultTwo, resultThree]) => (
)}
;
`
Install using npm:
``
npm install react-composer
or yarn:
``
yarn add react-composer
This library has one, default export: Composer.
Compose multiple render prop components together. The props are as
follows:
A render function that is called with an array of results accumulated from the render prop components.
`jsx`
{results => {
/ Do something with results... Return a valid React element. /
}}
The 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.
`jsx
// React elements may be passed for basic use cases
// props.children will be provided via React.cloneElement
// Render functions may be passed for added flexibility and control
({ results, render }) => (
)
]}>
{([outerResult, middleResult]) => {
/ Do something with results... Return a valid React element. /
}}
`
> Note: You do not need to provide props.children to the React element entries in props.components. If you do provide props.children to these elements, it will be ignored and overwritten.
#### props.components as render functions
A 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:
1. results: The currently accumulated results. You can use this for render prop components which depend on the results of other render prop components.render
2. : 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.
`jsx`
// props.components may contain both elements and render functions
({ / results, / render }) =>
]}>
{results => {
/ Do something with results... /
}}
`jsx`
({ results: [outerResult], render }) => (
),
({ results, render }) => (
)
// ...
]}>
{([outerResult, middleResult, innerResult]) => {
/ Do something with results... /
}}
By default, 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:
`jsx`
// Support varying named render props
({ render }) =>
({ render }) =>
({ render }) =>
// ...
]}>
{results => {
/ Do something with results... /
}}
Example of how to handle cases when a component passes multiple arguments to its render prop rather than a single argument.
`jsx`
// Differing render prop signature (multi-arg producers)
({ render }) => (
{(one, two) => render([one, two])}
),
]}>
{([outerResult, [one, two], innerResult]) => {
/ Do something with results... /
}}
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:
`jsx`
The first item in the components array will be the outermost component that is rendered. So, for instance,
if you pass
`jsx`
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.
`jsx`
]}
// ...
>
Alternatively, you can leverage the flexibility of the props.components as functions API and plug the render function in directly yourself.
`jsx``
({render}) =>
]}
// ...
>
Here are some examples of render prop components that benefit from React Composer:
* React's Context API. See this example by Kent Dodds.
* React Request
* Apollo's Query component
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.