source: imaps-frontend/node_modules/react-universal-interface/README.md@ d565449

main
Last change on this file since d565449 was d565449, checked in by stefan toskovski <stefantoska84@…>, 4 weeks ago

Update repo after prototype presentation

  • Property mode set to 100644
File size: 3.6 KB
Line 
1# react-universal-interface
2
3Easily create a component which is render-prop, Function-as-a-child and component-prop.
4
5```js
6import {render} from 'react-universal-interface';
7
8class MyData extends React.Component {
9 render () {
10 return render(this.props, this.state);
11 }
12}
13```
14
15Now 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
34Use 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
50Given a `<MyData>` component, it is said to follow **universal component interface** if, and only if, it supports
51all 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)
74const ChildWithData = withData(Child);
75
76// Decorator
77@withData
78class ChildWithData extends {
79 render () {
80 return <Child {...this.props.data} />;
81 }
82}
83```
84
85This library allows you to create universal interface components using these two functions:
86
87- `render(props, data)`
88- `createEnhancer(Comp, propName)`
89
90First, in your render method use `render()`:
91
92```js
93class MyData extends Component {
94 render () {
95 return render(this.props, data);
96 }
97}
98```
99
100Second, create enhancer out of your component:
101
102```js
103const withData = createEnhancer(MyData, 'data');
104```
105
106Done!
107
108
109## Installation
110
111<pre>
112npm 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
119import {render, createEnhancer} from 'react-universal-interface';
120```
121
122
123## Reference
124
125### `render(props, data)`
126
127- `props` &mdash; props of your component.
128- `data` &mdash; data you want to provide to your users, usually this will be `this.state`.
129
130
131### `createEnhancer(Facc, propName)`
132
133- `Facc` &mdash; FaCC component to use when creating enhancer.
134- `propName` &mdash; prop name to use when injecting FaCC data into a component.
135
136Returns a component enhancer `enhancer(Comp, propName, faccProps)` that receives three arguments.
137
138- `Comp` &mdash; required, component to be enhanced.
139- `propName` &mdash; optional, string, name of the injected prop.
140- `faccProps` &mdash; optional, props to provide to the FaCC component.
141
142
143## TypeScript
144
145TypeScript users can add typings to their render-prop components.
146
147```ts
148import {UniversalProps} from 'react-universal-interface';
149
150interface Props extends UniversalProps<State> {
151}
152
153interface State {
154}
155
156class MyData extends React.Component<Props, State> {
157}
158```
159
160
161## License
162
163[Unlicense](./LICENSE) &mdash; 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
Note: See TracBrowser for help on using the repository browser.