我有一个React Native应用,每天显示一个数组中的新报价。想法是您打开应用程序,它将显示您今天的报价。明天打开它,它将为您显示明天的新报价。
现在,我在顶级组件之一的mapStateToProps
方法中从Redux存储中获取当前报价,如下所示:
const mapStateToProps = state => {
const currentQuote = getCurrentQuote(
state
);
return {
quote: currentQuote
};
};
还有getCurrentQuote
:
const getCurrentQuote = (state, day) => {
const index = getDayOfYear(!day ? new Date() : new Date(day));
return state.quotes[index];
};
我的确切问题是,如果有人打开了该应用程序,并且日期已更改(即新的一天),则没有任何变化。没有新的报价。
很明显,我可以让用户手动刷新应用程序并更新报价,但我希望它可以自动更新。
除了不断运行setInterval
之外,组件还有一种更好的方式来响应new Date
的更改(例如,新的一天)。
可能AppState
https://facebook.github.io/react-native/docs/appstate.html