[关闭]
@iwanglong 2018-11-05T07:46:07.000000Z 字数 1106 阅读 276

React-native 之 NavigatorIOS 组件

技术


我们知道,在很多app中都有导航栏,那么RN中如何实现呢,这就用到NavigatorIOS组件(这个组件只适用于iOS),跨平台(cross-platform)解决方案请看Navigation或者native-Navigationreact-native-navigation

那么咱们就一步步的代码实现以下:
* 第一步
新建一个NavigatorIOSExample.js文件
* 第二步
直接下边代码搞起

  1. export default class NavigatorIOSExample extends Component {
  2. render() {
  3. return (
  4. <NavigatorIOS
  5. initialRoute={{
  6. component: MyScene,
  7. title: 'My Initial Scene',
  8. passProps: { title: 'test' },
  9. }}
  10. style={{ flex: 1 }} />
  11. )
  12. }
  13. }
  14. class MyScene extends Component {
  15. static propTypes = {
  16. title: PropTypes.string.isRequired,
  17. navigator: PropTypes.object.isRequired,
  18. }
  19. _onForward = () => {
  20. // alert(this.props.navigator.pop)
  21. this.props.navigator.push({
  22. component: Scene,
  23. title: 'Scene',
  24. leftButtonTitle: '返回',
  25. onLeftButtonPress: () => {
  26. this.props.navigator.pop();
  27. }
  28. });
  29. }
  30. render() {
  31. return (
  32. <View style={{ marginTop: 64 }}>
  33. <Text>Current Scene: {this.props.title}</Text>
  34. <TouchableHighlight onPress={this._onForward}>
  35. <Text>Tap me to load the next scene</Text>
  36. </TouchableHighlight>
  37. </View>
  38. )
  39. }
  40. }

NavigatorIOS 是通过route来处理子视图、props、navigation bar等。
上边initalRoute需要注意的是 :

  • component 必填
  • passProps 这个在官方第一个例子中没有对这个属性做处理,子组件就直接调用了this.props.title,会有warning警告说title is undefined。所以需要再passProps来对props处理。
添加新批注
在作者公开此批注前,只有你和作者可见。
回复批注