当前位置:网站首页>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 …)
边栏推荐
- 736. LISP syntax parsing: DFS simulation questions
- Baidu flying general BMN timing action positioning framework | data preparation and training guide (Part 2)
- mysqlbackup 还原特定的表
- C language instance_ four
- 长按按钮执行函数
- According to the analysis of the Internet industry in 2022, how to choose a suitable position?
- 安利一波C2工具
- 2022 Google CTF SEGFAULT LABYRINTH wp
- Yunna - work order management system and process, work order management specification
- ZOJ problem set – 2563 long dominoes [e.g. pressure DP]
猜你喜欢

子网划分、构造超网 典型题

Reptile practice (VI): novel of climbing pen interesting Pavilion

云呐|工单管理办法,如何开展工单管理

Yunna | work order management measures, how to carry out work order management

Gin 入门实战

2022 Google CTF segfault Labyrinth WP
![JS reverse -- ob confusion and accelerated music that poked the [hornet's nest]](/img/40/da56fe6468da64dd37d6b5b0082206.png)
JS reverse -- ob confusion and accelerated music that poked the [hornet's nest]

tansig和logsig的差异,为什么BP喜欢用tansig

云呐-工单管理制度及流程,工单管理规范

AcWing 1148. Secret milk transportation problem solution (minimum spanning tree)
随机推荐
AcWing 1140. 最短网络 (最小生成树)
IDEA常用的快捷键
736. Lisp 语法解析 : DFS 模拟题
C language instance_ five
AcWing 1142. 繁忙的都市 题解(最小生成树)
WCF基金会
Yunna | work order management software, work order management software app
安全保护能力是什么意思?等保不同级别保护能力分别是怎样?
Send template message via wechat official account
移植DAC芯片MCP4725驱动到NUC980
剑指 Offer II 035. 最小时间差-快速排序加数据转换
Yunna | work order management measures, how to carry out work order management
Wood extraction in Halcon
mysqlbackup 还原特定的表
2022 Google CTF SEGFAULT LABYRINTH wp
The cradle of eternity
Baidu flying general BMN timing action positioning framework | data preparation and training guide (Part 1)
使用nodejs完成判断哪些项目打包+发版
云呐-工单管理制度及流程,工单管理规范
域分析工具BloodHound的使用说明