0

动作触发但存储未更新,当动作在单个对象中时动作不起作用,带有返回方法的单独动作起作用

动作.js

export const actions = {
  GET_ORDERS_COUNT:'GET_ORDERS_COUNT'
};

order.js

class OrderDashboard extends React.Component {
    constructor(props) {
        super(props);
        this.state = {}
    }

    componentDidMount() {
        store.dispatch({
            type: actions.GET_ORDERS_COUNT
        });
    }
}

export default connect(mapStateToProps, {actions})(OrderDashboard);

减速器.js

const initState = {
    dashboardData:0
};

export default function (state = initState, action) {
    switch (action.type) {
        case actions.GET_ORDERS_COUNT: {
            return {
                ...state,
                dashboardData: 5,
            }
        }
    }
4

2 回答 2

0

this.props使用而不是怎么样store,并确保你import { connect } from react-redux

import { connect } from 'react-redux'
class OrderDashboard extends React.Component {
constructor(props) {
    super(props);
    this.state = {}

}

componentDidMount() {
    this.props.dispatch({
        type: actions.GET_ORDERS_COUNT
    });
}

}

export default connect(mapStateToProps, {actions})(OrderDashboard);
于 2019-02-01T07:13:32.737 回答
0

要使用store.dispatch(),您必须导入store. 但是我相信这是一种反模式,所以你需要使用this.props.dispatch(). 而且这条线export default connect(mapStateToProps, {actions})(OrderDashboard);是错误的。您需要并且需要在文件顶部export default connect(mapStateToProps)(OrderDashboard);导入您的。actions所以你的代码应该是这样的:

import {actions} from './actions';

class OrderDashboard extends React.Component {
constructor(props) {
    super(props);
    this.state = {}
}

 componentDidMount() {
     this.props.dispatch({type:actions.GET_ORDERS_COUNT})
 }

}

export default connect(mapStateToProps)(OrderDashboard);

另外,不知道为什么要在这里使用状态和构造函数,但是如果没有在代码中添加任何其他内容,则删除构造函数、超级和状态

于 2019-02-01T20:59:14.127 回答