美文网首页
在react中class和function组件调用dispatc

在react中class和function组件调用dispatc

作者: 兔子Tony的主人 | 来源:发表于2018-09-15 13:30 被阅读0次

copy党的疑问

function Checklist({ dispatch, list: dataSource, loading, total, page: current }) {

    function editHandler(id, values) {
        dispatch({
            type: 'checklist/patch',
            payload: { id, values },
        });
    }

如上代码,用dispatch调用models的数据方法,拷贝到class写的组件中,报dispatch不存在的错误。不知道该怎么解决

解决方案

dispatch({
    type: 'checklist/patch',
    payload: { id, values },
 });

修改为

this.props.dispatch({
    type: 'checklist/patch',
    payload: { id, values },
 });

就可以了

整理思路

对于dispatch这个东西来说,如果在function类型的组件中,要在参数中接收;如果在class类型的组件中直接用this.props.dispatch就可以使用。

相关文章

网友评论

      本文标题:在react中class和function组件调用dispatc

      本文链接:https://www.haomeiwen.com/subject/fogzgftx.html