how to use universal footer for all screens in nativebase

时光毁灭记忆、已成空白 提交于 2019-12-24 11:28:19

问题


I am new to react-native and I am using nativebase UI with React Navigation This a screen I have as home the main screen.

I am using nativebase components Container, Header, Footer.

My question is how can I create one Footer or Header file that I can import in all screens?

so I have one universal template for footer since it will be the same on all pages.

Code:

export default class Home extends React.Component {

  render() {
    return (
      <Container>
        <Header>
          <Left/>
          <Body>
            <Title>Home</Title>
          </Body>
          <Right />
        </Header>
        <Content>
          <Text>We have { this.props.screenProps.currentFriends.length } friends!</Text>
          <Button
            block
            onPress={() =>
              this.props.navigation.navigate('Friends')
            }
          >
          <Text>Add some friends</Text>
          </Button>
        </Content>

        <Footer>
          <FooterTab>
            <Button vertical active>
              <Icon name="apps" />
              <Text>Home</Text>
            </Button>
            <Button vertical>
              <Icon name="person" />
              <Text>Friends</Text>
            </Button>
          </FooterTab>
        </Footer>

      </Container>
    );
  }

}

回答1:


There can be two type of reusable component

  1. Stateful - in which reusable component (footer/header/etc) will have their own state
export default class MyFooter extends React.Component{

  render() {
    return (
      <Left>
        <Icon name="person" type="EvilIcon" />
        <Text>{props.name}</Text>
      </Left>
     );
   }
}
import MyFooter from "./MyFooter";  // <-- Don't forget to import

export default class YourScreen extends React.Component {

 constructor(props) {
    super(props);
  }

  render() {
    return (
       <Container>
        <Header/>
        <Content />
        <MyFooter name="Click Me" /> // <--- this is custom component
     );
   }
}
  1. Stateless - where it will wont have any state, just props these both components resides in same file YourScreen.js
const MyFooter = props => {
  if (props.name) {
    return (
      <Left>
        <Icon name="person" type="EvilIcon" />
        <Text>{props.name}</Text>
      </Left>
    );
  } else {
    return null;
  }
};

export default class YourScreen extends React.Component {

  render() {
    return (
       <Container>
        <Header/>
        <Content />
        <MyFooter name="Click Me" /> // <--- this is custom component
     );
   }
}


来源:https://stackoverflow.com/questions/55059348/how-to-use-universal-footer-for-all-screens-in-nativebase

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!