TabNavigator

时间:2017-09-17 13:47:22

标签: javascript android react-native react-navigation

我在初始屏幕上有从服务器加载的道具。我想将它们传递给其他选项卡屏幕。但是,我没有在网上找到任何例子。我知道screenProps,但不知道如何设置它。我尝试过的所有方法都会导致错误。

const EProj = TabNavigator({
  Home: { screen: HomeScreen },
  Map: { screen: MapG },
  Login: { screen: Login },
  Profile: { screen: Profile },
}, {
  tabBarPosition: 'bottom',
  animationEnabled: true,
  tabBarOptions: {
    activeTintColor: '#1abc9c',
  },
});

这是我的屏幕设置。我应该在哪里放置screenProps?

<EProj
  screenProps={cats}
/>

如何设置它的任何好例子都会有所帮助。提前谢谢。

HomeScreen设置:

class HomeScreen extends React.Component {
  static navigationOptions = {
    tabBarLabel: 'Home',
  };

...

  componentWillMount(){
    console.log("Starting to load assets from server!");
    this.onLoadCats(); /*starts asset loading*/
  }

  render() {
    return (
      <View style={styles.container}>

        <Text>Welcome to alpha 1.17 This is hard system test.</Text>
        <AssetsLoad catsL={this.state.catsL} />
      </View>
    );
  }
}

1 个答案:

答案 0 :(得分:20)

您可以做的是创建一个可以返回导航的高阶组件,在该组件的componentDidMount中,您可以加载数据并通过screenProps传递。

示例

const EProj = TabNavigator({
  Home: { screen: HomeScreen },
  Map: { screen: MapG },
  Login: { screen: Login },
  Profile: { screen: Profile },
}, {
  tabBarPosition: 'bottom',
  animationEnabled: true,
  tabBarOptions: {
    activeTintColor: '#1abc9c',
  },
});

class MainNavigation extends React.Component {
  constructor(props) {
    super(props);
    this.state = {cats: []};
  }
  componentDidMount() {
    this.onLoadCats().then((cats) => this.setState({ cats: cats }));
  }
  render() {
    return(<EProj screenProps={{ cats: this.state.cats}} />
  }
}

// Now you can do in your screens
console.log(this.props.screenProps.cats);

/* This is next line is wrong, please check update above */ 
/* console.log(this.props.navigation.state.params.cats); */