gpt4 book ai didi

reactjs - Redux - 从函数调用 Action

转载 作者:行者123 更新时间:2023-12-05 09:15:15 24 4
gpt4 key购买 nike

我正在尝试从函数中调用 redux 操作。我从中调用函数的组件已经连接到商店。但是,如果我通过以下操作,它就不起作用:

function myFunc(action) {
action()
}

有没有办法通过参数传递 Action ?谢谢。

最佳答案

使用 redux 中的 bindActionCreator 是从组件分派(dispatch)操作的最简单方法。

import { connect } from 'react-redux';
import { bindActionCreators } from 'redux';

class ReduxConnectedComponent extends React.Component {
mainFunction = () =>{
this.props.someAction(); // this dispatches your action.
}
render(){}
}
const mapStateToProps = store => ({
test: store.modules.someObject,
});

const mapDispatchToProps = dispatch => bindActionCreators({
someAction: yourActionCreatorFunction,
}, dispatch);
export default connect(maStateToProps,mapDispatchToProps)(ReduxConnectedComponent)

在 mapDispatchToProps 中,我们使用 bindActionCreator 将函数绑定(bind)到 redux store dispatch。这样无论何时调用它。它实际上调度存储操作

关于reactjs - Redux - 从函数调用 Action ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52906589/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com