React native 我应该在ComponentDidMount中设置状态吗?
我有AppContainer,它是另一个正在渲染的屏幕: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}
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
}...
]