Skip to content

Commit 42d4fa7

Browse files
committed
doc: 增加rematch使用文档
1 parent ac11e00 commit 42d4fa7

File tree

2 files changed

+116
-11
lines changed

2 files changed

+116
-11
lines changed

website/src/pages/docs/react-native-template/mock-data/README.md

+3-11
Original file line numberDiff line numberDiff line change
@@ -55,10 +55,7 @@ const Demo = ({ update }) => {
5555
},
5656
})
5757

58-
const { mutate, isLoading } = login({
59-
update,
60-
formData,
61-
})
58+
const { mutate, isLoading } = login({ formData })
6259

6360
return (
6461
<Button
@@ -73,12 +70,7 @@ const Demo = ({ update }) => {
7370
);
7471
}
7572

76-
export default connect(
77-
({}) => ({}),
78-
({ global }) => ({
79-
update: global.update
80-
}),
81-
)(Demo);
73+
export default Demo
8274

8375
```
8476
### 三、使用react-query调用api
@@ -89,7 +81,7 @@ import { userLogin } from '../services/users';
8981
import { useQuery, useMutation } from 'react-query'
9082

9183
// 登录
92-
export const login = ({ config = {}, update, formData }) => {
84+
export const login = ({ config = {}, formData }) => {
9385
const mutation = useMutation({
9486
mutationFn: userLogin,
9587
onSuccess: async (data) => {

website/src/pages/docs/react-native-template/new-page/README.md

+113
Original file line numberDiff line numberDiff line change
@@ -93,3 +93,116 @@ const styles = StyleSheet.create({
9393
})
9494
```
9595
具体请参照官方文档[样式](https://reactnative.cn/docs/style)
96+
97+
## Rematch
98+
99+
### 第一步:建立models
100+
101+
```bash
102+
mocker
103+
...
104+
src
105+
models
106+
+ home.js
107+
pages
108+
...
109+
package.json
110+
```
111+
112+
```js
113+
export default {
114+
name: 'home',
115+
// initial state
116+
state: {
117+
num:0
118+
},
119+
reducers: {
120+
update: (state, payload) => ({ ...state, ...payload }),
121+
},
122+
effects: (dispatch) => ({
123+
// 可以进行异步请求
124+
}),
125+
};
126+
```
127+
128+
### 第二步:初始化store
129+
130+
```js
131+
import { init } from '@rematch/core';
132+
import createLoadingPlugin from '@rematch/loading';
133+
import * as home from './home';
134+
135+
136+
const loadingPlugin = createLoadingPlugin({});
137+
138+
export const store = init({
139+
models: {
140+
home: home.default,
141+
},
142+
plugins: [loadingPlugin],
143+
});
144+
145+
```
146+
147+
### 第三步:页面中使用
148+
```js
149+
import React, { useState } from 'react';
150+
import { View,Text } from 'react-native'
151+
import { Button } from '@uiw/react-native';
152+
import { connect } from 'react-redux';
153+
154+
const Demo = ({ num,update }) => {
155+
const click = ()=>{
156+
update({
157+
num:10
158+
})
159+
}
160+
return (
161+
<View>
162+
<Text>{num}</Text>
163+
<Button onPress={click}>点击</Button>
164+
</View>
165+
);
166+
}
167+
168+
export default connect(
169+
({ home }) => ({
170+
num:home.num
171+
}),
172+
({ home }) => ({
173+
update: home.update
174+
}),
175+
)(Demo);
176+
177+
```
178+
179+
我们也可以使用hooks调用
180+
```js
181+
import React, { useState } from 'react';
182+
import { View,Text } from 'react-native'
183+
import { Button } from '@uiw/react-native';
184+
import { useSelector,useDispatch } from 'react-redux';
185+
186+
const Demo = () => {
187+
const dispatch = useDispatch();
188+
const { num } =useSelector(state=>state.home)
189+
const click = ()=>{
190+
dispatch({
191+
type:'home/update',
192+
payload:{
193+
num:10
194+
}
195+
})
196+
}
197+
return (
198+
<View>
199+
<Text>{num}</Text>
200+
<Button onPress={click}>点击</Button>
201+
</View>
202+
);
203+
}
204+
205+
export default Demo
206+
207+
```
208+
具体请参照官方文档[Rematch](https://rematchjs.org/docs/)

0 commit comments

Comments
 (0)