11# react-render-callback
22
3- > render anything (Function as Child, Render Props, Components, Elements, ...)
4-
5- ---
3+ > render-prop helper to render anything (Functions, Components, Elements, ...)
64
75[ ![ version] [ version-badge ]] [ package ]
86[ ![ MIT License] [ license-badge ]] [ license ]
2422## The problem
2523
2624You want your component to support the [ ` render prop ` ] [ render-prop ] [ pattern] [ use-a-render-prop ]
27- and you want to support several different types of values like
25+ with different types of values like
2826[ Function as children] [ function-as-children ] ,
29- a [ React.Component] [ react-component ]
27+ a [ React.Component] [ react-component ] (Component Injection)
3028or just plain react elements.
3129
3230## This solution
3331
34- ` react-render-callback ` frees you from detecting what kind of callback your component is dealing with:
32+ ` react-render-callback ` frees you from detecting what kind fo [ ` render prop ` ] [ render-prop ]
33+ your component is dealing with:
3534
3635``` js
3736import React from ' react'
37+ import renderCallback from ' react-render-callback'
3838
39- import render from ' react-render-callback'
40-
39+ // children may be a function, a component, an element, ...
4140class Component from React .Component {
4241 state = {}
4342
4443 render () {
45- // can be any prop: return render(this.props. renderHeader, this.state.header)
46- return render (this .props .children , this .state )
44+ // can be any prop like render, component, renderHeader, ...
45+ return renderCallback (this .props .children , this .state )
4746 }
4847}
4948```
5049
51- It can render the following types:
52-
53- - [ Stateless Function Components (SFC)] ( https://reactjs.org/docs/components-and-props.html#functional-and-class-components )
54- with one argument (the common ` props ` case) aka _ Render Props Pattern_
55- or [ optional with several arguments] ( #use-createrender-to-pass-down-several-arguments )
56- - [ Class Components] ( https://reactjs.org/docs/react-component.html ) aka _ Component Injection Pattern_
57- - [ Context] ( https://reactjs.org/docs/context.html ) Provider and Consumer
58- - [ Forward Refs] ( https://reactjs.org/docs/react-api.html#reactforwardref )
59- - [ Factories] ( https://reactjs.org/docs/react-api.html#createfactory )
60- - [ Elements] ( https://reactjs.org/docs/glossary.html#elements )
61- with [ optional support] ( #use-optionscloneelement ) for [ cloning] [ clone-element ] to merge props
62- - primitives like strings, numbers, arrays, ...
63- - ` false ` , ` null ` , ` undefined ` and ` true ` are returned as ` null `
64- just like in [ JSX] ( https://reactjs.org/docs/jsx-in-depth.html#booleans-null-and-undefined-are-ignored )
65-
6650View an example in [ codesandbox.io] ( https://codesandbox.io/s/48k5p1r764?module=%2FApp.js ) .
6751
52+ ## Highlights
53+
54+ - :package : Super tiny (~ 600 bytes)
55+ - :ok_hand : Dependency free (except for [ Object.assign] ( https://developer.mozilla.org/de/docs/Web/JavaScript/Reference/Global_Objects/Object/assign ) polyfill)
56+ - :electric_plug : Plug and play
57+ - :crystal_ball : Tree shaking friendly (ESM, no side effects)
58+ - :books : Well documented
59+ - :100 : test coverage
60+ - :family : supports rendering of
61+ - [ Stateless Function Components (SFC)] ( https://reactjs.org/docs/components-and-props.html#functional-and-class-components )
62+ with one argument (the common ` props ` case) aka _ Render Props_ aka _ Function as Child_
63+ or [ optional with several arguments] ( #use-createrender-to-pass-down-several-arguments )
64+ - [ Class Components] ( https://reactjs.org/docs/react-component.html ) aka _ Component Injection_
65+ - [ Context] ( https://reactjs.org/docs/context.html ) Provider and Consumer
66+ - [ Forward Refs] ( https://reactjs.org/docs/react-api.html#reactforwardref )
67+ - [ Factories] ( https://reactjs.org/docs/react-api.html#createfactory )
68+ - [ Elements] ( https://reactjs.org/docs/glossary.html#elements )
69+ with [ optional support] ( #use-optionscloneelement ) for [ cloning] [ clone-element ] to merge props
70+ - primitives like strings, numbers, arrays, ...
71+ - ` false ` , ` null ` , ` undefined ` and ` true ` are returned as ` null `
72+ just like in [ JSX] ( https://reactjs.org/docs/jsx-in-depth.html#booleans-null-and-undefined-are-ignored )
73+
6874## Table of Contents
6975
7076<!-- START doctoc generated TOC please keep comment here to allow auto update -->
@@ -104,15 +110,15 @@ via [unpkg.com](https://unpkg.com/) and exposed as `ReactRenderCallback`.
104110
105111### API
106112
107- #### ` render ([ renderable [, props [, options ] ] ])`
113+ #### ` renderCallback ([ renderable [, props [, options ] ] ])`
108114
109115> renders the given ` renderable ` with ` props `
110116
111117``` js
112118// esm
113- import render from ' react-render-callback'
119+ import renderCallback from ' react-render-callback'
114120// commonjs
115- const render = require (' react-render-callback' )
121+ const renderCallback = require (' react-render-callback' )
116122```
117123
118124** renderable** (optional): anything that can be rendered like a function, a component, or elements
@@ -141,10 +147,10 @@ const render = require('react-render-callback')
141147 the element using [ ` React.cloneElement ` ] [ clone-element ]
142148
143149``` js
144- render (< a href= " #bar" > bar< / a> , {title: ' foo' })
150+ renderCallback (< a href= " #bar" > bar< / a> , {title: ' foo' })
145151// --> <a href="#bar">bar</a>
146152
147- render (< a href= " #bar" > bar< / a> , {title: ' foo' }, {cloneElement: true })
153+ renderCallback (< a href= " #bar" > bar< / a> , {title: ' foo' }, {cloneElement: true })
148154// --> <a href="#bar" title="foo">bar</a>
149155```
150156
@@ -167,21 +173,21 @@ import {createRender} from 'react-render-callback'
167173const {createRender } = require (' react-render-callback' )
168174```
169175
170- Accepts the same arguments (except ` props ` ) as ` render ()` . It exists mainly
176+ Accepts the same arguments (except ` props ` ) as ` renderCallback ()` . It exists mainly
171177to pre-determine (read cache) what type ` renderable ` is, to prevent these
172178checks on every invocation.
173179
174180Additionally the returned method accepts more than one argument (since: v1.2.0).
175- This allows to provide several parameters to the render function .
181+ This allows to provide several parameters to the ` renderable ` .
176182
177183``` js
178- const render = createRender ((a , b , c ) => ({a, b, c}))
179- render (1 , 2 , 3 )
184+ const renderCallback = createRender ((a , b , c ) => ({a, b, c}))
185+ renderCallback (1 , 2 , 3 )
180186// -> { a: 1, b: 2, c: 3 }
181187```
182188
183- If only one argument is passed and it is a plain object, that argument is merged
184- with (if defined) the ` defaultProps ` of the ` renderable ` .
189+ > If the ` renderable ` has a ` defaultProps ` property only the first parameter is used
190+ > and merged with the ` defaultProps ` .
185191
186192** returns**
187193
@@ -301,7 +307,7 @@ const App = () => <Toggle>{ToggleView}</Toggle>
301307
302308## Other Solutions
303309
304- - [ ` render-props ` ] ( https://github. com/donavon /render-props )
310+ - [ ` render-props ` ] ( https://www.npmjs. com/package /render-props )
305311- [ ` react-render-function ` ] ( https://www.npmjs.com/package/react-render-function )
306312- [ ` @macklinu/render-props ` ] ( https://www.npmjs.com/package/@macklinu/render-props )
307313
0 commit comments