1 | # react-universal-interface
|
---|
2 |
|
---|
3 | Easily create a component which is render-prop, Function-as-a-child and component-prop.
|
---|
4 |
|
---|
5 | ```js
|
---|
6 | import {render} from 'react-universal-interface';
|
---|
7 |
|
---|
8 | class MyData extends React.Component {
|
---|
9 | render () {
|
---|
10 | return render(this.props, this.state);
|
---|
11 | }
|
---|
12 | }
|
---|
13 | ```
|
---|
14 |
|
---|
15 | Now you can use it:
|
---|
16 |
|
---|
17 | ```jsx
|
---|
18 | <MyData render={(state) =>
|
---|
19 | <MyChild {...state} />
|
---|
20 | } />
|
---|
21 |
|
---|
22 | <MyData>{(state) =>
|
---|
23 | <MyChild {...state} />
|
---|
24 | }</MyData>
|
---|
25 |
|
---|
26 | <MyData comp={MyChild} />
|
---|
27 | <MyData component={MyChild} />
|
---|
28 | ```
|
---|
29 |
|
---|
30 | ---
|
---|
31 |
|
---|
32 | [![][npm-badge]][npm-url] [![][travis-badge]][travis-url] [![React Universal Interface](https://img.shields.io/badge/React-Universal%20Interface-green.svg)](https://github.com/streamich/react-universal-interface)
|
---|
33 |
|
---|
34 | Use this badge if you support universal interface:
|
---|
35 |
|
---|
36 | <div align="center">
|
---|
37 | <a href="https://github.com/streamich/react-universal-interface">
|
---|
38 | <img src="https://img.shields.io/badge/React-Universal%20Interface-green.svg" />
|
---|
39 | </a>
|
---|
40 | </div>
|
---|
41 |
|
---|
42 | ```
|
---|
43 | [![React Universal Interface](https://img.shields.io/badge/React-Universal%20Interface-green.svg)](https://github.com/streamich/react-universal-interface)
|
---|
44 | ```
|
---|
45 |
|
---|
46 |
|
---|
47 | ---
|
---|
48 |
|
---|
49 |
|
---|
50 | Given a `<MyData>` component, it is said to follow **universal component interface** if, and only if, it supports
|
---|
51 | all the below usage patterns:
|
---|
52 |
|
---|
53 | ```jsx
|
---|
54 | // Function as a Child Component (FaCC)
|
---|
55 | <MyData>{
|
---|
56 | (data) => <Child {...data} />
|
---|
57 | }</MyData>
|
---|
58 |
|
---|
59 | // Render prop
|
---|
60 | <MyData render={
|
---|
61 | (data) => <Child {...data} />
|
---|
62 | } />
|
---|
63 |
|
---|
64 | // Component prop
|
---|
65 | <MyData component={Child} />
|
---|
66 | <MyData comp={Child} />
|
---|
67 |
|
---|
68 | // Prop injection
|
---|
69 | <MyData>
|
---|
70 | <Child />
|
---|
71 | </MyData>
|
---|
72 |
|
---|
73 | // Higher Order Component (HOC)
|
---|
74 | const ChildWithData = withData(Child);
|
---|
75 |
|
---|
76 | // Decorator
|
---|
77 | @withData
|
---|
78 | class ChildWithData extends {
|
---|
79 | render () {
|
---|
80 | return <Child {...this.props.data} />;
|
---|
81 | }
|
---|
82 | }
|
---|
83 | ```
|
---|
84 |
|
---|
85 | This library allows you to create universal interface components using these two functions:
|
---|
86 |
|
---|
87 | - `render(props, data)`
|
---|
88 | - `createEnhancer(Comp, propName)`
|
---|
89 |
|
---|
90 | First, in your render method use `render()`:
|
---|
91 |
|
---|
92 | ```js
|
---|
93 | class MyData extends Component {
|
---|
94 | render () {
|
---|
95 | return render(this.props, data);
|
---|
96 | }
|
---|
97 | }
|
---|
98 | ```
|
---|
99 |
|
---|
100 | Second, create enhancer out of your component:
|
---|
101 |
|
---|
102 | ```js
|
---|
103 | const withData = createEnhancer(MyData, 'data');
|
---|
104 | ```
|
---|
105 |
|
---|
106 | Done!
|
---|
107 |
|
---|
108 |
|
---|
109 | ## Installation
|
---|
110 |
|
---|
111 | <pre>
|
---|
112 | npm i <a href="https://www.npmjs.com/package/react-universal-interface">react-universal-interface</a> --save
|
---|
113 | </pre>
|
---|
114 |
|
---|
115 |
|
---|
116 | ## Usage
|
---|
117 |
|
---|
118 | ```js
|
---|
119 | import {render, createEnhancer} from 'react-universal-interface';
|
---|
120 | ```
|
---|
121 |
|
---|
122 |
|
---|
123 | ## Reference
|
---|
124 |
|
---|
125 | ### `render(props, data)`
|
---|
126 |
|
---|
127 | - `props` — props of your component.
|
---|
128 | - `data` — data you want to provide to your users, usually this will be `this.state`.
|
---|
129 |
|
---|
130 |
|
---|
131 | ### `createEnhancer(Facc, propName)`
|
---|
132 |
|
---|
133 | - `Facc` — FaCC component to use when creating enhancer.
|
---|
134 | - `propName` — prop name to use when injecting FaCC data into a component.
|
---|
135 |
|
---|
136 | Returns a component enhancer `enhancer(Comp, propName, faccProps)` that receives three arguments.
|
---|
137 |
|
---|
138 | - `Comp` — required, component to be enhanced.
|
---|
139 | - `propName` — optional, string, name of the injected prop.
|
---|
140 | - `faccProps` — optional, props to provide to the FaCC component.
|
---|
141 |
|
---|
142 |
|
---|
143 | ## TypeScript
|
---|
144 |
|
---|
145 | TypeScript users can add typings to their render-prop components.
|
---|
146 |
|
---|
147 | ```ts
|
---|
148 | import {UniversalProps} from 'react-universal-interface';
|
---|
149 |
|
---|
150 | interface Props extends UniversalProps<State> {
|
---|
151 | }
|
---|
152 |
|
---|
153 | interface State {
|
---|
154 | }
|
---|
155 |
|
---|
156 | class MyData extends React.Component<Props, State> {
|
---|
157 | }
|
---|
158 | ```
|
---|
159 |
|
---|
160 |
|
---|
161 | ## License
|
---|
162 |
|
---|
163 | [Unlicense](./LICENSE) — public domain.
|
---|
164 |
|
---|
165 |
|
---|
166 | [npm-url]: https://www.npmjs.com/package/react-universal-interface
|
---|
167 | [npm-badge]: https://img.shields.io/npm/v/react-universal-interface.svg
|
---|
168 | [travis-url]: https://travis-ci.org/streamich/react-universal-interface
|
---|
169 | [travis-badge]: https://travis-ci.org/streamich/react-universal-interface.svg?branch=master
|
---|