当前位置:网站首页>Basic introduction and use of dvajs
Basic introduction and use of dvajs
2022-07-07 01:40:00 【Idle fish_ JavaScript】
Directory structure :
1. Synchronously modify the warehouse value
- src/models/LhTest.ts: Where you define the warehouse
export default {
// Warehouse inventory value ;
state: {
global: ' This is a global',
},
// Synchronous operation place : Modify component common state
reducers: {
// This is your modification state Methods ; Fixed format
changeGlobal(state: Object, {
payload }: any) {
console.log(' This is you through payload The parameters passed in :', payload)
return {
...state,
global: payload,
};
},
},
};
- src/pages/index.tsx: Page call
import {
connect } from 'dva';
import {
Button } from 'antd';
import React,{
useState } from 'react';
const IndexPage = (props: any) => {
// from props To deconstruct global and dispatch Method
const {
global, dispatch } = props;
const [numb, setNumb] = useState(0);
const changeGlobal = (numb:any) => {
setNumb(numb + 1)
// This method is in props Inside
dispatch({
// type: ' You define model name , The default is your file name , You can also customize it : namespace'
// type: / Then your method name , From effects perhaps reducers Go inside ; If the names are the same, then effects In looking for ( That is to say effects Find first in ; If you find it, you'll end the search );
type: 'LhTest/changeGlobal',
// This place is where you spread your knowledge !
payload: numb++
})
}
return (
<>
<Button onClick={
changeGlobal.bind({
}, numb)}>ce {
global }</Button>
</>
);
}
// This step You can think of it this way , stay IndexPage The previous step of page creation ,dva Add a step to it, that is connect The first callback function passed in .
export default connect((state: any) => {
// LhTest yes modal name The default is the file name or your own definition namespace ;
const {
global } = state.LhTest;
return {
global
};
// connect() This function returns a function Accept a page to be rendered ;
})(IndexPage);
2. Modify the warehouse value asynchronously
- src/models/LhTest.ts: Where you define the warehouse
import {
request } from 'umi';
// data Is the passed parameter
const user = (data:any) => {
return request(`/user`, {
method: 'POST',
// Header information
headers: {
},
// json Format
requestType: 'json',
// post Request is for data, get Request is for params
data
});
}
export default {
// Warehouse inventory value ;
state: {
global: ' This is a global',
},
// Synchronous operation place : Modify component common state
reducers: {
// This is your modification state Methods ; Fixed format ; When called ' Module name / Method name '
changeGlobal(state: Object, {
payload }: any) {
console.log(' This is you through payload The parameters passed in :', payload)
return {
...state,
global: payload,
};
},
// In order to test effects Of put Method
handleSomethingReducers(state: Object, {
payload }: any) {
console.log('reducers Methods the trigger ,payload:', payload)
return {
...state
}
}
},
// Asynchronous operation place : Request interface
effects: {
// Method name , When called ' Module name / Method name '
// payload: The parameters you passed ;
*getUserInfo({
payload }: any, {
call, put }: any) {
// call: The asynchronously requested function accepts two parameters ( first 【user】: Functions called asynchronously 【 Request interface 】 the second 【payload】: Pass parameters into your asynchronous function )
// Return interface information
const {
data } = yield call(user, payload);
// Call other methods ( for instance reducers Inside Or is it effects Inside , Or other .)
yield put({
type: 'handleSomethingReducers', payload: {
name: 1 } }); // reducers Methods the trigger ,payload: {name: 1}
yield put({
type: 'handleSomethingEffects', payload: {
name: 2 } }); // effects Methods the trigger ,payload: {name: 2}
},
// In order to test effects Of put Method
*handleSomethingEffects({
payload}:any, {
call, put}: any) {
console.log('effects Methods the trigger ,payload:', payload)
}
},
};
- src/pages/index.tsx: Page call
import {
connect } from 'dva';
import {
Button } from 'antd';
import React,{
useState } from 'react';
const IndexPage = (props: any) => {
// from props To deconstruct global and dispatch Method
const {
global, dispatch } = props;
const [numb, setNumb] = useState(0);
const changeGlobal = (numb:any) => {
setNumb(numb + 1)
// This method is in props Inside
dispatch({
// type: ' You define model name , The default is your file name , You can also customize it : namespace'
// type: / Then your method name , From effects perhaps reducers Go inside ; If the names are the same, then effects In looking for ( That is to say effects Find first in ; If you find it, you'll end the search );
type: 'LhTest/changeGlobal',
// This place is where you spread your knowledge !
payload: numb++
})
dispatch({
type: 'LhTest/getUserInfo',
// This place is where you spread your knowledge !
payload: {
name: 'zs',
age: '46'
}
})
}
return (
<>
<Button onClick={
changeGlobal.bind({
}, numb)}>ce {
global }</Button>
</>
);
}
// This step You can think of it this way , stay IndexPage The previous step of page creation ,dva Add a step to it, that is connect The first callback function passed in .
export default connect((state: any) => {
// LhTest yes modal name The default is the file name or your own definition namespace ;
const {
global } = state.LhTest;
return {
global
};
// connect() This function returns a function Accept a page to be rendered ;
})(IndexPage);
3. Namespace
- You can get multiple folders , It will read by default file Name as default namespace;
- Modular custom name , Normally, the file name is used as the module , But when the name repeats ? Will report a mistake ! Then use namespaces namespace Customize it
The solution is as follows :
add to namespace after
Not added namespace
Print the final result :connect((state)=> {console.log(‘state:’, state)})(IndexPage)
3.xxxx( be on holiday , To be continued …)
边栏推荐
- JS reverse -- ob confusion and accelerated music that poked the [hornet's nest]
- AcWing 1140. Shortest network (minimum spanning tree)
- 糊涂工具类(hutool)post请求设置body参数为json数据
- Baidu flying general BMN timing action positioning framework | data preparation and training guide (Part 2)
- Vocabulary in Data Book
- C语言关于链表的代码看不懂?一篇文章让你拿捏二级指针并深入理解函数参数列表中传参的多种形式
- Set WordPress pseudo static connection (no pagoda)
- Wood extraction in Halcon
- AcWing 1140. 最短网络 (最小生成树)
- Installation of gazebo & connection with ROS
猜你喜欢
AI 从代码中自动生成注释文档
Wood extraction in Halcon
1123. The nearest common ancestor of the deepest leaf node
云呐-工单管理制度及流程,工单管理规范
AcWing 361. 观光奶牛 题解(spfa求正环)
According to the analysis of the Internet industry in 2022, how to choose a suitable position?
对C语言数组的再认识
云呐|工单管理软件,工单管理软件APP
Installation of gazebo & connection with ROS
Set WordPress pseudo static connection (no pagoda)
随机推荐
AcWing 1141. LAN problem solving (kruskalkruskal finding the minimum spanning tree)
Appium自动化测试基础 — uiautomatorviewer定位工具
C语言实例_5
制作带照明的DIY焊接排烟器
Google发布安全更新,修复Chrome中已被利用的0 day
Set WordPress pseudo static connection (no pagoda)
JVM 内存模型
405 method not allowed appears when the third party jumps to the website
js如何快速创建一个长度为 n 的数组
AcWing 345. 牛站 题解(floyd的性质、倍增)
According to the analysis of the Internet industry in 2022, how to choose a suitable position?
C language instance_ three
454-百度面经1
AcWing 344. Solution to the problem of sightseeing tour (Floyd finding the minimum ring of undirected graph)
Make Jar, Not War
JS reverse -- ob confusion and accelerated music that poked the [hornet's nest]
AcWing 1140. 最短网络 (最小生成树)
Baidu flying general BMN timing action positioning framework | data preparation and training guide (Part 1)
C language instance_ five
AcWing 361. 观光奶牛 题解(spfa求正环)