Skip to content

Commit

Permalink
update docs
Browse files Browse the repository at this point in the history
  • Loading branch information
awmleer committed Oct 29, 2019
1 parent 39396bf commit 3be5777
Show file tree
Hide file tree
Showing 2 changed files with 0 additions and 117 deletions.
59 changes: 0 additions & 59 deletions docs/advanced.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,65 +18,6 @@ export function FooStore(initial = 1) {
</Provider>
```

## withProvider

Sometimes we need to use a store in the same component that provides it, for example:

```jsx
export function App(props) {
const fooStore = useStore(FooStore) // ⚠️Can't get fooStore here
return (
<Provider of={FooStore}>
<p>{fooStore.x}</p>
</Provider>
)
}
```

We hope to create a `Provider` of `FooStore` in `App` component, and call `useStore(FooStore)` in it at the same time. OK, we got `withProvider` for you:

```jsx
export const App = withProvider({
of: FooStore
})((props) => {
const fooStore = useStore(FooStore) // 🎉 Now we can get fooStore here
return (
<p>{fooStore.x}</p>
)
})
```

`withProvider` is a HOC(Higher-Order Component). First, we pass a props object to it(just like what we do in jsx, but in the format of object). And then we pass our component to it.

```jsx
withProvider({
of: FooStore,
args: [42, 'abc'],
})(MyComponent)
```

If you want to generate the `props` of `Provider` dynamically, you can pass a **function** to `withProvider`.

```jsx
withProvider(props => ({
of: FooStore,
args: [42, props.id],
}))(MyComponent)
```


What's more,you can build your own Higher-Order Components based on `withProvider`:

```js
const withFooStoreProvider = withProvider({
of: FooStore
})

export const App = withFooStoreProvider((props) => {
//...
})
```

## Use in Class Components

Even though Reto itself is written with hooks, it is still supported to use Reto in class components. You may wonder how to use `useStore` in class components. The answer is: No, you can't. But, there is an substitute for `useStore`, which is `Consumer` component:
Expand Down
58 changes: 0 additions & 58 deletions docs/zh-cn/advanced.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,64 +18,6 @@ export function FooStore(initial = 1) {
</Provider>
```

## withProvider

有时,我们需要在一个组件中"同时"提供和使用某个store,例如:

```jsx
export function App(props) {
const fooStore = useStore(FooStore) // ⚠️在这里拿不到fooStore
return (
<Provider of={FooStore}>
<p>{fooStore.x}</p>
</Provider>
)
}
```

我们希望在`App`组件中创建`FooStore``Provider`,但又同时想在`App`组件中`useStore(FooStore)`。这时,我们可以使用`withProvider`

```jsx
export const App = withProvider({
of: FooStore
})((props) => {
const fooStore = useStore(FooStore) // 🎉可以正常获取到fooStore了
return (
<p>{fooStore.x}</p>
)
})
```

`withProvider`分为两层,你需要先传给它Provider的属性(将jsx中Provider的props写成object的形式),然后再传给它你想要加工的组件。

```jsx
withProvider({
of: FooStore,
args: [42, 'abc'],
})(MyComponent)
```

此外,你还可以传入一个props的**生成函数**。如果你希望根据组件接收到到`props`动态控制传递给`Provider``props`,可以使用这种方法。

```jsx
withProvider(props => ({
of: FooStore,
args: [42, props.id],
}))(MyComponent)
```

当然,你还可以基于`withProvider`创建自己的高阶组件:

```js
const withFooStoreProvider = withProvider({
of: FooStore
})

export const App = withFooStoreProvider((props) => {
//...
})
```

## 在类组件中使用

虽然Reto自身是通过hooks实现的,但是也是支持在类组件中使用的。显然,我们不能在类组件中使用`useStore`,但是Reto提供了可以在类组件中使用的`Consumer`
Expand Down

0 comments on commit 3be5777

Please sign in to comment.