How does react dva change state
This article mainly introduces how react dva changes state, has a certain reference value, interested friends can refer to, I hope you can learn a lot after reading this article, the following let the editor take you to understand it.
React dva changes the state method: 1, create DvaState.js and dvaState.js files; 2, enter the code as "import React from 'react';import {connect} from.}".
The operating environment of this paper: Windows7 system, react17.0.1, Dell G3.
Click on the react dva framework to modify the value of state similar to this.state and this.setState () in react.
Show me first.
Click to change the value of state before
The change of the value of state after clicking change
First, the creator has two files.
DvaState.js under the routes directory
Import React from 'react';import {connect} from' dva';import {Link} from 'dva/router' Let count = 0const DvaState = ({dispatch, dvaState,}) = > {const {dataList} = dvaState console.log (dataList) console.log (dvaState) const handleChangeState = () = > {dispatch ({type:'dvaState/changeState', payload: {dataList: [{list1: count++, list2: 'list22', list3:' list33'}) {list2:'111'}]}})} return (
DvaState {dataList [0] .list1} change state
);}; export default connect (({dvaState}) = > ({dvaState})) (DvaState)
DvaState.js under the models directory
Export default {namespace: 'dvaState', state: {dataList: [{list1:'a', list2:'c',}]}, subscriptions: {setup ({dispatch, history}) {},}, effects: {}, reducers: {changeState (state, {payload}) {return {... state,. Payload}},} Thank you for reading this article carefully. I hope the article "how to change state in react dva" shared by the editor will be helpful to everyone. At the same time, I also hope that you will support us and pay attention to the industry information channel. More related knowledge is waiting for you to learn!