Skip to content

Commit c36960e

Browse files
authored
chore: improve readme
1 parent b9a6aa6 commit c36960e

File tree

1 file changed

+71
-14
lines changed

1 file changed

+71
-14
lines changed

README.md

Lines changed: 71 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,14 @@
66

77
![](https://i.imgflip.com/27euu2.jpg)
88

9+
## 📜 Table content
10+
11+
- [Why](#why)
12+
- [Usage](#usage)
13+
- [Working with new Context api](#working-with-new-context-api)
14+
- [Custom render and retrieving props from composed](#custom-render-and-retrieving-props-from-composed)
15+
- [Typescript support](#typescript-support)
16+
917
## 🧐   Why
1018

1119
[Render Props](https://reactjs.org/docs/render-props.html) are the new hype of React's ecossystem, that's a fact. So, when you need to use more than one render props component together, this can be boring and generate something called a *"render props callback hell"*, like that:
@@ -17,7 +25,7 @@
1725
* **Small**. 0.7kb minified!
1826
* **Extremely Simple**. Just a method!
1927

20-
React Adopt is just a simple method that you can compose your components and return all props in a function by mapping each child prop returned by your component.
28+
React Adopt is just a simple method that you can compose your components and return just one component that will be a render prop component that combining each prop result from your mapper.
2129

2230
## 💻   Usage ([demo](https://codesandbox.io/s/vq1wl37m0y?hidenavigation=1))
2331

@@ -31,6 +39,27 @@ Now you can use adopt to compose your components. See above an example using the
3139

3240
![Good](https://i.imgur.com/RXVlFwy.png)
3341

42+
### Working with new Context api
43+
44+
One of use case that React Adopt can fit perfectly is when you need to use [new React's context api](https://reactjs.org/docs/context.html) that use render props to create some context:
45+
46+
```js
47+
import React from 'react'
48+
import { adopt } from 'react-adopt'
49+
50+
const ThemeContext = React.createContext('light')
51+
const UserContext = React.createContext({ name: 'John' })
52+
53+
const Context = adopt({
54+
theme: <ThemeContext.Consumer />,
55+
user: <UserContext.Consumer />,
56+
})
57+
58+
<Context>
59+
{({ theme, user }) => /* ... */}
60+
</Context>
61+
```
62+
3463
### Custom render and retrieving props from composed
3564

3665
Some components don't use the `children` property as render props. For cases like that, you can pass a function as mapper value that will return your component. This function will receive as props the `render` method, the props passed on `Composed` component and the previous values from each mapper. See an example:
@@ -43,13 +72,11 @@ const Composed = adopt({
4372
custom: ({ render }) => <MyCustomRenderProps render={render} />
4473
})
4574

46-
const App = () => (
47-
<Composed>
48-
{({ custom }) => (
49-
<div>{custom.value}</div>
50-
)}
51-
</Composed>
52-
)
75+
<Composed>
76+
{({ custom }) => (
77+
<div>{custom.value}</div>
78+
)}
79+
</Composed>
5380
```
5481

5582
And as I said above, you can retrieve the properties passed to the composed component using that way too:
@@ -65,13 +92,43 @@ const Composed = adopt({
6592
)
6693
})
6794

68-
const App = () => (
69-
<Composed initialGreet="Hi">
70-
{({ greet }) => (
71-
<div>{greet.value}</div>
72-
)}
73-
</Composed>
95+
<Composed initialGreet="Hi">
96+
{({ greet }) => (
97+
<div>{greet.value}</div>
98+
)}
99+
</Composed>
100+
```
101+
102+
### Typescript support
103+
104+
React adopt has a fully typescript support when you need to type the composed component:
105+
106+
```ts
107+
import * as React from 'react'
108+
import { adopt } from 'react-adopt'
109+
import { Value } from 'react-powerplug'
110+
111+
interface RenderProps {
112+
foo: { value: string }
113+
}
114+
115+
interface Props {
116+
tor: string
117+
}
118+
119+
const foo = ({ tor, render }) => (
120+
<Value initial="foo">{render}</Value>
74121
)
122+
123+
const Composed = adopt<RenderProps, Props>({
124+
foo,
125+
})
126+
127+
<Composed tor="tor">
128+
{({ foo, bar }) => (
129+
<div>{foo.value}</div>
130+
)}
131+
</Composed>
75132
```
76133

77134
## 🕺 &nbsp; Contribute

0 commit comments

Comments
 (0)