reactjs - 如何正确延迟渲染
问题描述
//离线.js
import { offline } from '@redux-offline/redux-offline';
import offlineConfig from '@redux-offline/redux-offline/lib/defaults';
const config = params => ({
...offlineConfig,
persistCallback: params.persistCallback
});
export default params => offline(config(params));
//App.js
class App extends Component {
componentWillMount() {
this.store = Reactotron.createStore(
reducers,
undefined,
compose(
applyMiddleware(ReduxThunk),
offline({ persistCallback: this.startApp })
)
);
}
startApp = () => {
if (this.store.getState().auth.loggedIn) {
const resetAction = StackActions.reset({
index: 0,
actions: [NavigationActions.navigate({ routeName: 'summmary' })],
});
this.props.navigation.dispatch(resetAction);
}
}
render() {
return (
<Provider store={this.store}>
<MainNavigator />
</Provider>
);
}
}
export default App;
我有 2 个屏幕summary
,默认情况下将在login
哪里渲染屏幕。login
但是我添加了这个逻辑this.store.getState().auth.loggedIn
来检查它是否为真然后将屏幕发送到summary
但我得到了Cannot read property 'dispatch' of undefined
解决方案
导航道具可用于包装在 MainNavigator 中的 Screen 组件。
您正在尝试从导航器之外的 App 组件中使用它,因此它是未定义的。
在这种情况下,建议使用文档中描述的“不使用导航道具导航”方法: https ://reactnavigation.org/docs/en/navigating-without-navigation-prop.html
推荐阅读
- php - Insert result in DB using laravel
- python - Create a 10 minute running average of data with uneven time steps
- gradle - Gradle多项目buildSrc在子项目中不起作用
- count - Excluding Values
- python - 使用 conda 安装 plotly 时遇到问题
- reactjs - 有没有办法在 firestore collection() 和 doc() 中使用 React 状态?
- xgboost - H2O XGBoost 因本地服务器死机或挂起而崩溃(?)
- c# - URL重写是用两个段重写我的所有文件
- google-chrome-extension - 如何在 Chrome 扩展中使用 API 从 Google 表格中读取数据?
- java - java.time.LocalDate 范围内的生日