![](/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.