All Projects → jamesplease → React Composer

jamesplease / React Composer

Licence: mit
Compose render prop components

Programming Languages

javascript
184084 projects - #8 most used programming language

Projects that are alternatives of or similar to React Composer

react-translations-provider
React render props component for setting translations
Stars: ✭ 35 (-94.26%)
Mutual labels:  render, props
Magiskhidepropsconf
MagiskHidePropsConf
Stars: ✭ 441 (-27.7%)
Mutual labels:  props
okeevis-render
a fast lightweight 2d graphic library
Stars: ✭ 22 (-96.39%)
Mutual labels:  render
React Scope
Visualize your React components as you interact with your application.
Stars: ✭ 316 (-48.2%)
Mutual labels:  props
try3d
Attempt on WebGL, refinement of rendering technology.
Stars: ✭ 37 (-93.93%)
Mutual labels:  render
Wpf Math
.NET library for rendering mathematical formulae using the LaTeX typsetting style, for the WPF framework
Stars: ✭ 339 (-44.43%)
Mutual labels:  render
react-tabllist
React-based customizable style table or list components that support event and callback functions.
Stars: ✭ 20 (-96.72%)
Mutual labels:  props
React Virtual List
Super simple virtualized list React component
Stars: ✭ 597 (-2.13%)
Mutual labels:  render
Breakdance
It's time for your markup to get down! HTML to markdown converter. Breakdance is a highly pluggable, flexible and easy to use.
Stars: ✭ 418 (-31.48%)
Mutual labels:  render
Shouldcomponentupdate Children
React "Shallow Equal" HOC implementation to optimize shouldComponentUpdate with children / React elements 🐇➰
Stars: ✭ 272 (-55.41%)
Mutual labels:  props
Seapig
🌊🐷 Utility for generalized composition of React components
Stars: ✭ 269 (-55.9%)
Mutual labels:  props
mitsuba-visualize
Visualizes meshes, pointclouds and video flythroughs in publication quality
Stars: ✭ 67 (-89.02%)
Mutual labels:  render
Glyph Brush
Fast GPU cached text rendering
Stars: ✭ 353 (-42.13%)
Mutual labels:  render
laravel-mjml
Laravel MJML offers support for rendering MJML syntax into in-line HTML that can be sent within mails.
Stars: ✭ 26 (-95.74%)
Mutual labels:  render
Lighthouse2
Lighthouse 2 framework for real-time ray tracing
Stars: ✭ 542 (-11.15%)
Mutual labels:  render
minecraft-overviewer
Docker Image to Run Minecraft Overviewer
Stars: ✭ 60 (-90.16%)
Mutual labels:  render
Unityurp Mobilescreenspaceplanarreflection
Reusable RendererFeature of MobileScreenSpacePlanarReflection
Stars: ✭ 260 (-57.38%)
Mutual labels:  render
Vue Types
Vue Prop Types definitions
Stars: ✭ 331 (-45.74%)
Mutual labels:  props
React Fake Props
🔮 Magically generate fake props for your React tests
Stars: ✭ 604 (-0.98%)
Mutual labels:  props
Opencue
A render management system you can deploy for visual effects and animation productions.
Stars: ✭ 578 (-5.25%)
Mutual labels:  render

React Composer

Travis build status npm version npm downloads Test Coverage gzip size

Compose render prop components.

Motivation

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>;

Installation

Install using npm:

npm install react-composer

or yarn:

yarn add react-composer

API

This library has one, default export: Composer.

<Composer />

Compose multiple render prop components together. The props are as follows:

props.children

A 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.components

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.

<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.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.
  2. 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>

Examples and Guides

Example: Render prop component(s) depending on the result of other render prop component(s)

<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>

Example: Render props named other than 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: Render prop component(s) that produce multiple arguments

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>

Limitations

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} />

Render Order

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

Console Warnings

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} />
  ]}
  // ...
>

Example Usage

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!

Contributing

Are you interested in helping out with this project? That's awesome – thank you! Head on over to the contributing guide to get started.

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].