问题
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
- 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
);
}
}
- 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