案例是我有三个屏幕,显示从 API 获取的结果,并允许用户对这些结果调度操作。这些动作触发(应该)导致其他两个屏幕。换句话说,如果用户在任何屏幕上并执行了一些操作,那么其他两个屏幕应该更新。
例如,屏幕 A、B 和 C。我可以执行以下两种方法之一:
- 条件渲染:
class MainScreen extends Component {
state: Object;
constructor(props) {
super(props);
this.state = { currentActiveScreen: 1 }
}
componentWillMount()
{
this.retrieveResultForScreenA();
this.retrieveResultForScreenB();
this.retrieveResultForScreenC();
}
retrieveResultForScreenA()
{
// get results from API
}
retrieveResultForScreenB()
{
// get results from API
}
retrieveResultForScreenC()
{
// get results from API
}
ChangeScreen(screen_number)
{
this.setState({currentActiveScreen: screen_number});
}
render()
{
if(this.state.currentActiveScreen === 1)
{
// render screen A results
// along with a tab bar to switch screens:
<View style={{flexDirection: "row"}}>
<TouchableOpacity onPress={()=>{ this.ChangeScreen.bind(this, 1) }}>
<Text>ScreenA</Text>
</TouchableOpacity>
<TouchableOpacity onPress={()=>{ this.ChangeScreen.bind(this, 2) }}>
<Text>ScreenB</Text>
</TouchableOpacity>
<TouchableOpacity onPress={()=>{ this.ChangeScreen.bind(this, 3) }}>
<Text>ScreenC</Text>
</TouchableOpacity>
</View>
}
if(this.state.currentActiveScreen === 2)
{
// render screen B results
// along with a tab bar to switch screens:
<View style={{flexDirection: "row"}}>
<TouchableOpacity onPress={()=>{ this.ChangeScreen.bind(this, 1) }}>
<Text>ScreenA</Text>
</TouchableOpacity>
<TouchableOpacity onPress={()=>{ this.ChangeScreen.bind(this, 2) }}>
<Text>ScreenB</Text>
</TouchableOpacity>
<TouchableOpacity onPress={()=>{ this.ChangeScreen.bind(this, 3) }}>
<Text>ScreenC</Text>
</TouchableOpacity>
</View>
}
if(this.state.currentActiveScreen === 3)
{
// render screen C results
// along with a tab bar to switch screens:
<View style={{flexDirection: "row"}}>
<TouchableOpacity onPress={()=>{ this.ChangeScreen.bind(this, 1) }}>
<Text>ScreenA</Text>
</TouchableOpacity>
<TouchableOpacity onPress={()=>{ this.ChangeScreen.bind(this, 2) }}>
<Text>ScreenA</Text>
</TouchableOpacity>
<TouchableOpacity onPress={()=>{ this.ChangeScreen.bind(this, 3) }}>
<Text>ScreenA</Text>
</TouchableOpacity>
</View>
}
}
}
- 三个屏幕的 TabNavigator:
class ScreenA extends Component {
static navigationOptions = ({ navigation }) => ({ title: 'ScreenA' });
constructor(props) {
super(props);
}
componentWillMount()
{
this.retrieveResultForScreenA();
}
retrieveResultForScreenA()
{
// get results from API
}
render() {
return (
// render screen A results
);
}
}
class ScreenB extends Component {
static navigationOptions = ({ navigation }) => ({ title: 'ScreenB' });
constructor(props) {
super(props);
}
componentWillMount()
{
this.retrieveResultForScreenB();
}
retrieveResultForScreenA()
{
// get results from API
}
render() {
return (
// render screen B results
);
}
}
class ScreenC extends Component {
static navigationOptions = ({ navigation }) => ({ title: 'ScreenC' });
constructor(props) {
super(props);
}
componentWillMount()
{
this.retrieveResultForScreenC();
}
retrieveResultForScreenA()
{
// get results from API
}
render() {
return (
// render screen C results
);
}
}
const MainScreen = TabNavigator({
ScreenA: { screen: MyScreenA },
ScreenB: { screen: MyScreenB },
ScreenC: { screen: MyScreenC },
});
第一种方法的问题在于:
- 如果用户切换屏幕,即使用户没有在任何屏幕上发送任何操作,应用程序也会获取并使用网络
第二种方法的问题在于:
- 其他选项卡不会在任何调度的操作上更新(tabNavigator 为所有屏幕呈现一次,仅此而已)
如何结合这两种方法并拥有干净的代码和最新的屏幕?