加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

reactjs – 如何根据redux商店的内容在React Navigation中动态指

发布时间:2020-12-15 20:30:44 所属栏目:百科 来源:网络整理
导读:我的导航器是: import { connect } from 'react-redux';import { createDrawerNavigator } from 'react-navigation';const screens = { Home: Homecreen,};if (this.props.userStatus !== 'active') { screens.Upgrade = UpgradeScreen;}const MainDrawer =
我的导航器是:

import { connect } from 'react-redux';
import { createDrawerNavigator } from 'react-navigation';

const screens = {
  Home: Homecreen,};
if (this.props.userStatus !== 'active') {
  screens.Upgrade = UpgradeScreen;
}

const MainDrawer = createDrawerNavigator(screens);

const mapStateToProps = (state) => {
  return { userStatus: state.user.status };
};

export default connect(mapStateToProps)(MainDrawer);

我想仅在用户状态未激活时才显示UpgradeScreen.在上面的代码中,我收到错误:

undefined is not an object (evaluating 'this.props.userStatus')

我该如何解决?

解决方法

要访问this.props.userStatus,您应该有一个组件.这是你如何做到的:

import { connect } from 'react-redux';
import { createDrawerNavigator } from 'react-navigation';

const screens = {
  Home: Homecreen,};

const MainDrawer = ({ userStatus }) => {
  if (userStatus !== 'active') {
    screens.Upgrade = UpgradeScreen;
  }

  const Navigator = createDrawerNavigator(screens);

  return <Navigator />
}

const mapStateToProps = (state) => {
  return { userStatus: state.user.status };
};

export default connect(mapStateToProps)(MainDrawer);

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读