我在下面的頁面陣營:凡持有狀態,一個回調後一直存在,直到一些運行
@connect((store) => {
return {
all_orders_data: store.trends.all_orders_data,
...etc...
};
})
class OrdersController extends MyComponent {
constructor(props){
let custom_methods = ['refreshData', 'runDispatch'];
super(props, custom_methods);
this.state = {last_updated_at: new Date()};
}
componentWillMount() {
this.runDispatch();
// this.props.dispatch(fetchOrderData());
}
componentDidMount() {
this.refreshTimerID = setInterval(
() => this.refreshData(),
300 * 1000 // 5 minutes
);
}
refreshData() {
console.log('running refresh');
this.runDispatch(true);
this.setState({last_updated_at: new Date()});
}
runDispatch(refresh=false) {
this.props.dispatch(fetchOrderTrendingAllOrdersData(refresh));
}
render() {
return (
<div>
<h1>Order Trending</h1>
{last_updated_at}
<br/>
<div className="card col-md-12">
<h2 className="style-1">All Orders</h2>
<LineChart
data={all_orders_data}
fetched={fetched_all_orders_data}
error={error_in_fetching_all_orders_data}
/>
</div>
</div>
)
}
在渲染我解開道具,last_updated
頭,並呈現一個折線圖。
我希望能夠點擊的按鈕切換圖表上線。要做到這一點,我必須跟蹤data
變量的密鑰以顯示爲行。
我不能把這些行選項放在構造函數中,因爲runDispatch
直到componentWillMount才被觸發。
我不能把它放在componentWillMount中,因爲我不知道runDispatch何時會返回數據直到它返回。
我可以在reducer中獲得all_orders_data
的密鑰並將其作爲道具傳遞給OrdersController
,但道具無法更改,我希望我們的應用的此頁控制當前顯示的行。
我不想把它放在圖表組件上,因爲它每次刷新運行都會得到新的道具,我不知道它是否會在刷新後保持正確的狀態。
切換設置不需要稍後保留,只有當控制器處於活動狀態(如果他們選擇新的鏈接,我不在乎它是否重置)。
我的直覺是把狀態放在折線圖上,因爲它不必是永久的。
哪裏是正確的位置,以保持這些線路鍵的狀態,如:
{
all_orders: ['Online orders', 'Web orders', ...]
}
讓用戶切換,他希望看到的圖表是什麼線?它可以在LineChart
,控制器,一個新的redux道具等。