How do i load my React component conditionally?

帅比萌擦擦* 提交于 2019-12-08 06:22:24

Looks like you got a typo error you added ; after null this is unneeded also you can get rid of the getToolbar function Instead try:

constructor(props) {
  super(props);
  this.state = {
      currentpagenum: 0,
  };
}

render() {
  return(
      <View>
          {this.state.currentpagenum !== 0 ? <ToolbarAndroid /> : null}
      </View>
   );
}

Another way to render something conditionally is to do this:

render() {
  return(
      <View>
          {this.state.currentpagenum !== 0 && <ToolbarAndroid />}
      </View>
   );
}

Which of course due to how 'truthiness' works in javascript, means you could shorten that further to this:

render() {
  return(
      <View>
          {this.state.currentpagenum && <ToolbarAndroid />}
      </View>
   );
}
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!