Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/search/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
React native 我应该在ComponentDidMount中设置状态吗?_React Native_React Navigation - Fatal编程技术网

React native 我应该在ComponentDidMount中设置状态吗?

React native 我应该在ComponentDidMount中设置状态吗?,react-native,react-navigation,React Native,React Navigation,我有AppContainer,它是另一个正在渲染的屏幕: class AppContainer extends Component { state= { Home: false } renderFooterTab = () => { return this.footerItems.map((tabBarItem, index) => { return ( <GlobalFooterTab key={index}

我有AppContainer,它是另一个正在渲染的屏幕:

class AppContainer extends Component {

state= {
  Home: false
}

renderFooterTab = () => {
    return this.footerItems.map((tabBarItem, index) => {
      return (
        <GlobalFooterTab
          key={index}
          title={tabBarItem.title}
          selected={tabBarItem.selected}
        />
      );
    });
  };

render() {
    return (
      <Container>
        <StatusBar />
        {this.renderHeader(this.props)}
        <Content {...this.props} contentContainerStyle={{ flexGrow: 1 }}>
          {this.props.children}
        </Content>
        {this.renderFooter(this.props)}
      </Container>
    );

footerItems = [
    {
      screen: 'home',
      title: 'Home,
      selected: this.state.isHome
    }...
]
}

实际上没有必要使用
state
,搜索“避免将道具复制到状态”是不可取的。相反,只要继续使用道具

const { routeName } = this.props.navigation.state;

footerItems = [
  {
    screen: 'home',
    title: 'Home,
    selected: routeName === 'Home'
  }...
]
如果你真的想,你可以这样使用“”:

const { routeName } = this.props.navigation.state;
const isHome = routeName === 'Home';

...

footerItems = [
  {
    screen: 'home',
    title: 'Home,
    selected: isHome
  }...
]

const { routeName } = this.props.navigation.state;
const isHome = routeName === 'Home';

...

footerItems = [
  {
    screen: 'home',
    title: 'Home,
    selected: isHome
  }...
]