Skip to content

Commit

Permalink
Update README.md
Browse files Browse the repository at this point in the history
  • Loading branch information
yisar authored Apr 12, 2020
1 parent e87b350 commit 9c86949
Showing 1 changed file with 12 additions and 7 deletions.
19 changes: 12 additions & 7 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -234,28 +234,33 @@ Context is no need to build in fre core. It can be implemented in user land and
Here is an example: [use-context-selector](https://github.com/yisar/fre/blob/master/demo/src/use-context.js)

```js
import { createContext, useContext } from 'fre/compat'

const Context = createContext(0)
const Context = createContext({
count1: 0,
count2: 0
})

function App() {
const [count, setCount] = useReducer(c => c + 1, 0)
const [count, setCount] = useState(Context.value)
return (
<Context.Provider value={count}>
<A />
<B />
<button onClick={() => setCount(count + 1)}>+</button>
<button onClick={() => setCount({ ...count, count1: count.count1 + 1 })}>
+
</button>
</Context.Provider>
)
}

function A() {
const context = useContext(Context, ctx => ctx) // with selector
const context = useContext(Context, ctx => ctx.count1)
console.log('A')
return <div>{context}</div>
}

function B() {
const context = useContext(Context, ctx => ctx)
const context = useContext(Context, ctx => ctx.count2)
console.log('B')
return <div>{context}</div>
}
```
Expand Down

0 comments on commit 9c86949

Please sign in to comment.