我已经阅读了这篇文章(https://medium.com/@stowball/a-dummys-guide-to-redux-and-thunk-in-react-d8904a7005d3)几次,但我仍然不确定如何在组件内调用您的操作。
我已经正确设置了我的动作和减速器(我希望),所以我现在处于最后一步,即使用您的动作。我知道我首先必须导入我的操作,然后使用connectfrom redux 将它连接到商店。
我目前在我的组件中有这个:
componentDidMount() {
this.serverRequest = axios
.get('http://helloworld/customers')
.then(res => {
// Rerender state
this.setState({
res,
dataToDisplay: res.data
})
})
}
Run Code Online (Sandbox Code Playgroud)
所以我的问题是:如何在组件内部使用我的操作?
与其他任何事情一样,您必须通过道具将动作创建者传递给您的组件。你可以通过使用 react-redux 库和 mapDispatchToProps 函数来做到这一点。
您需要先“绑定”您的动作创建者,方法是将其包装在另一个对您的动作创建者调用 dispatch 的函数中。
我强烈建议阅读以下 redux 文档,因为它很好地解释了它并提供了示例。
https://redux.js.org/basics/usage-with-react#implementing-container-components
| 归档时间: |
|
| 查看次数: |
9778 次 |
| 最近记录: |