export interface stateProps { count: number } export default { namespace: ‘project‘, state: { count: 0, // 数量 }, reducers: { changeCount: (state: stateProps, { payload: count }: {payload: {count: number}}) => { return { ...state, count: count } } }, }
import React from ‘react‘; import { connect } from ‘dva‘; const Products = ({ dispatch, count }) => { // 点击+1 const add = () => { dispatch({ type: ‘project/changeCount‘, payload: count + 1, }); } return ( <div> <h2>List of Products</h2> <div>数量{count}</div> <Button type="primary" onClick={add} >增加数量</Button> </div> ); }; // export default Products; export default connect(({ project }) => ({ count: project.count, }))(Products);
import React from ‘react‘; import { connect } from ‘dva‘; const TypeInfo = ({ count }) => { return ( <div> <h2>List of TypeInfo</h2> <div>产品数量{count}</div> </div> ); }; // export default TypeInfo; export default connect(({ project }) => ({ count: project.count, }))(TypeInfo);
原文:https://www.cnblogs.com/steamed-twisted-roll/p/14794479.html