diff options
author | Jamie Kyle <me@thejameskyle.com> | 2019-05-14 02:31:32 +0300 |
---|---|---|
committer | Jamie Kyle <me@thejameskyle.com> | 2019-05-14 02:31:32 +0300 |
commit | f75e4529109640e39e527cdadda66d5e748ab1bf (patch) | |
tree | 0edacda1d2ad832e034a86d5c47c540d37c43f0d /README-zh-cn.md | |
parent | badab5b6aba05a76abeb5e87fc4c531fd1a04d68 (diff) |
add initialState
Diffstat (limited to 'README-zh-cn.md')
-rw-r--r-- | README-zh-cn.md | 29 |
1 files changed, 26 insertions, 3 deletions
diff --git a/README-zh-cn.md b/README-zh-cn.md index a6b028d..33bc67e 100644 --- a/README-zh-cn.md +++ b/README-zh-cn.md @@ -45,8 +45,8 @@ import React, { useState } from "react" import { createContainer } from "unstated-next" import { render } from "react-dom" -function useCounter() { - let [count, setCount] = useState(0) +function useCounter(initialState = 0) { + let [count, setCount] = useState(initialState) let decrement = () => setCount(count - 1) let increment = () => setCount(count + 1) return { count, decrement, increment } @@ -69,7 +69,13 @@ function App() { return ( <Counter.Provider> <CounterDisplay /> - <CounterDisplay /> + <Counter.Provider initialState={2}> + <div> + <div> + <CounterDisplay /> + </div> + </div> + </Counter.Provider> </Counter.Provider> ) } @@ -106,6 +112,23 @@ function ParentComponent() { } ``` +### `<Container.Provider initialState>` + +```js +function useCustomHook(initialState = "") { + let [value, setValue] = useState(initialState) + // ... +} + +function ParentComponent() { + return ( + <Container.Provider initialState={"value"}> + <ChildComponent /> + </Container.Provider> + ) +} +``` + ### `Container.useContainer()` ```js |