https://segmentfault.com/a/1190000020288369?utm_source=tag-newest
https://github.com/umijs/umi-example-dva-user-dashboard

connect

mapStateToProps
mapDispatchToProps
https://blog.csdn.net/suwu150/article/details/79415085

hooks connect

  1. import React, { useEffect, useState } from 'react';
  2. import { connect } from 'dva';
  3. function TableList() {}
  4. function mapStateToProps({list, loading}) {
  5. return {
  6. list,
  7. loading: loading.effects['list/getList'] ?? false,
  8. }
  9. }
  10. function mapDispatchToProps(dispatch) {
  11. return {
  12. _$getList: payload => dispatch({ type: 'list/getList', payload }),
  13. }
  14. }
  15. export default connect(mapStateToProps, mapDispatchToProps)(TableList);

class connect

  1. import React, { PureComponent } from 'react';
  2. import { injectIntl } from 'react-intl';
  3. import { connect } from 'dva';
  4. @connect(
  5. state => ({list: state.list}),
  6. dispatch => ({
  7. _$getList: payload => dispatch({ type: 'list/getList', payload }),
  8. })
  9. );
  10. @injectIntl;
  11. class TableList extends PureComponent {
  12. componentDidMount() {}
  13. }
  1. import React, { useEffect, useState } from 'react';
  2. import { connect } from 'dva';
  3. import { routerRedux } from 'dva/router';
  4. function App(props) {
  5. console.log(props)
  6. useEffect(init, []);
  7. function init() {
  8. dispatch({
  9. type: 'monitor/init',
  10. payload: {page: 1, limit: 10 },
  11. })
  12. }
  13. function pageChange(id) {
  14. dispatch(routerRedux.push({
  15. pathname: '/users',
  16. query: { id },
  17. }));
  18. }
  19. return (
  20. <div>
  21. <button onClick={() => pageChange(3)}>查看用户详情</button>
  22. </div>
  23. )
  24. }
  25. function mapStateToProps(state) {
  26. const { list, total, page } = state.users;
  27. return {
  28. list,
  29. total,
  30. page,
  31. loading: state.loading.models.users,
  32. };
  33. }
  34. export default connect(mapStateToProps, null)(App);

image.png

mapStateToProps

state,是 dvamoel里面的所有命名空间,全局的命名空间;可以结构出想要的命名空间。

  1. function mapStateToProps(state, prevProps) {
  2. }

image.png

init

  1. function init() {
  2. dispatch({
  3. type: 'monitor/init',
  4. payload: {page: 1, limit: 10 },
  5. })
  6. }

models

reducers

  1. import { getData } from '@/services/monitor'
  2. export default {
  3. namespace: 'monitor',
  4. state: {
  5. dataSource: [],
  6. },
  7. reducers: {
  8. save(state, action) {
  9. console.log(123, action);
  10. return { ...state, ...(action.payload || {}) };
  11. },
  12. },
  13. effects: {
  14. *init({ payload }, { call, put }) {
  15. console.log('init', payload)
  16. const res = yield call(getData, payload);
  17. if(!res) return;
  18. yield put({ type: 'save', payload: res });
  19. },
  20. },
  21. }

reducer/save, action的参数
image.png