当前位置:网站首页>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 …)
边栏推荐
猜你喜欢

C语言关于链表的代码看不懂?一篇文章让你拿捏二级指针并深入理解函数参数列表中传参的多种形式

云呐|工单管理软件,工单管理软件APP

AcWing 345. 牛站 题解(floyd的性质、倍增)

ClickHouse字段分组聚合、按照任意时间段粒度查询SQL

Yunna | work order management software, work order management software app

百度飞将BMN时序动作定位框架 | 数据准备与训练指南 (下)

AI 从代码中自动生成注释文档

【信号与系统】

Send template message via wechat official account

shell脚本快速统计项目代码行数
随机推荐
Gin 入门实战
一起看看matlab工具箱内部是如何实现BP神经网络的
字符串的相关编程题
Comparison of picture beds of free white whoring
Share a general compilation method of so dynamic library
云呐|工单管理软件,工单管理软件APP
JS reverse -- ob confusion and accelerated music that poked the [hornet's nest]
AcWing 1140. Shortest network (minimum spanning tree)
AcWing 1148. 秘密的牛奶运输 题解(最小生成树)
C语言实例_5
Set WordPress pseudo static connection (no pagoda)
AcWing 346. 走廊泼水节 题解(推公式、最小生成树)
糊涂工具类(hutool)post请求设置body参数为json数据
454 Baidu Mianjing 1
[advanced C language] 8 written questions of pointer
百度飞将BMN时序动作定位框架 | 数据准备与训练指南 (下)
C语言实例_4
ZOJ Problem Set – 2563 Long Dominoes 【如压力dp】
454-百度面经1
拖拽改变顺序