![](/img/trans.png)
[英]How can I maintain my store's state while using react-router and redux?
[英]How to migrate state passed in React-Router to Redux Store?
到目前为止,我一直在使用React
和React-Router
一起使用React
,并且在URL
之间传递数据时,我一直在传递状态为
this.props.history.push(Routes.transactionEdit, this.state.transaction);
在组件中,我将其用作
constructor(props) {
super(props);
this.state = {
userId: props.history.location.state.userId,
transactionId: props.history.location.state.id,
createdAt: props.history.location.state.createdAt,
name: props.history.location.state.name,
amount: props.history.location.state.amount,
}
}
但是,我了解到在Redux
中管理状态是一个更好的主意,它有助于与视图外观分开测试商店。 我喜欢这个主意,因为我关心可清洁测试的代码。
我开始重构代码,对于需要从数据库获取数据的许多部分,我能够使用thunk
模式迁移代码。 但是,在这种用例中,状态是作为history.push
一部分发送的,我不知道如何将该状态放入Redux
Store。
题
如何在this.props.history.push(Routes.transactionEdit, this.state.transaction);
迁移状态this.props.history.push(Routes.transactionEdit, this.state.transaction);
(在推送期间)到Redux
存储,以便可以在React
Component之外对其进行管理?
谢谢
您需要设置一个特定的action
,该操作专门用于获取组件的状态并将其推送到Redux的存储。 当然,您需要使减速器对该动作做出适当的响应。 如果您是Redux的新手,这可能会打开一大堆蠕虫。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.