我是React Native的新手,并创建了一个包含三个按钮标签的页脚。我现在想知道如何通过单击按钮来呈现不同的屏幕。我的代码:
export default class Uptown extends Component {
render() {
return (
<Container>
<Header>
<Title>Uptown</Title>
</Header>
<Content>
<App />
</Content>
<Footer>
<FooterTab>
<Button>
Contact
</Button>
<Button>
Here
</Button>
<Button>
Me
</Button>
</FooterTab>
</Footer>
</Container>
);
}
}
按下按钮时如何更改屏幕?
答案 0 :(得分:9)
您可以添加条件呈现而不是静态<App/>
标记。您可以使用以下代码根据选定的页脚有条件地呈现。 (我使用状态变量来存储选定的页面索引。当索引改变时,引擎自动调用渲染函数)
import First from './Home' // your first screen
import Next from './Next' // your second screen
class Updown extends Component {
constructor(props) {
super(props)
this.state = {index: 0} // default screen index
}
switchScreen(index) {
this.setState({index: index})
}
render() {
let AppComponent = null;
if (this.state.index == 0) {
AppComponent = First
} else {
AppComponent = Second
}
return (
<Container>
<Header><Title> Header... </Title></Header>
<Content> <AppComponent/> </Content>
<Footer>
<Button onPress={() => this.switchScreen(0) }> First </Button>
<Button onPress={() => this.switchScreen(1) }> Second </Button>
</Footer>
</Container>
)
}
}
答案 1 :(得分:2)
我认为从原生基础v2.3.1(2017年8月)开始,推荐的方法是遵循documentation on React Navigation / TabNavigator。当然,只有你真的打算使用TabNavigator
(我这样做)才能使用React Navigation的好处(我不够了解那些是什么)。
但是,如果您希望页脚选项卡成为应用程序主导航结构的一部分,而不是一个页面的临时机制,那么这似乎是要走的路。
import React, { Component } from "react";
import LucyChat from "./LucyChat.js";
import JadeChat from "./JadeChat.js";
import NineChat from "./NineChat.js";
import { TabNavigator } from "react-navigation";
import { Button, Text, Icon, Footer, FooterTab } from "native-base";
export default (MainScreenNavigator = TabNavigator(
{
LucyChat: { screen: LucyChat },
JadeChat: { screen: JadeChat },
NineChat: { screen: NineChat }
},
{
tabBarPosition: "bottom",
tabBarComponent: props => {
return (
<Footer>
<FooterTab>
<Button
vertical
active={props.navigationState.index === 0}
onPress={() => props.navigation.navigate("LucyChat")}>
<Icon name="bowtie" />
<Text>Lucy</Text>
</Button>
<Button
vertical
active={props.navigationState.index === 1}
onPress={() => props.navigation.navigate("JadeChat")}>
<Icon name="briefcase" />
<Text>Nine</Text>
</Button>
<Button
vertical
active={props.navigationState.index === 2}
onPress={() => props.navigation.navigate("NineChat")}>
<Icon name="headset" />
<Text>Jade</Text>
</Button>
</FooterTab>
</Footer>
);
}
}
));
答案 2 :(得分:1)
您需要创建当前索引并绑定该功能以从选项卡切换到另一个选项卡。 要做到这一点:
request.user
希望有所帮助:)
答案 3 :(得分:1)
我认为我们可以查看@Hossein Mobasher代码的某些部分
import First from './Home' // your first screen
import Next from './Next' // your second screen
class Updown extends Component {
constructor(props) {
super(props)
this.state = {index: 0} // default screen index
}
switchScreen(index) {
this.setState({index: index})
}
render() {
let AppComponent = null;
if (this.state.index == 0) {
AppComponent = First
} else {
AppComponent = Second
}
return (
<Container>
<Header><Title> Header... </Title></Header>
<Content>
<AppComponent/> // you can write like this
</Content>
<Footer>
<Button onPress={() => this.switchScreen(0) }> First </Button>
<Button onPress={() => this.switchScreen(1) }> Second </Button>
</Footer>
</Container>
)
}
}