为什么需要 this.props.componentId?

pvi*_*nis 7 react-native react-native-navigation

为什么this.props.componentId需要?

它的目的是什么?

为什么我们不能在不需要那个 id 的情况下使用这个库?

react-navigation不需要那样的东西,react-native-navigationv1 没有使用那样的东西。那么为什么 v2 需要并使用它呢?我问的原因首先是为了理解它,其次是看看我是否可以跳过这个,因为现在我不能使用传奇中的 RNN v2。

小智 -2

这是库开发人员博文中的详细回答。react-native-navigation

\n\n
\n

所以现在我们要启用以下行为:用户单击文本后,应用程序会推送屏幕ViewPost。稍后将很容易将相同的功能附加到列表项而不是文本。要将新屏幕推入此 screen\xe2\x80\x99s 导航堆栈,我们将使用Navigation.push. 在新 API 中,此方法期望接收当前的 componentId,可以在 props.componentID 中找到。因此,PostsList.js我们创建一个pushViewPostScreen函数并将其附加到文本的 onPress 事件。

\n
\n\n

\r\n
\r\n
import React, {PureComponent} from \'react\';\r\nimport {View, Text} from \'react-native-ui-lib\';\r\nimport PropTypes from \'prop-types\';\r\nimport {Navigation} from \'react-native-navigation\';\r\n\r\nclass PostsList extends PureComponent {\r\n\r\n  static propTypes = {\r\n    navigator: PropTypes.object,\r\n    componentId: PropTypes.string\r\n  };\r\n\r\n  constructor(props) {\r\n    super(props);\r\n\r\n    this.pushViewPostScreen = this.pushViewPostScreen.bind(this);\r\n  }\r\n\r\n  pushViewPostScreen() {\r\n    // We pass the componentId to Navigation.push\r\n    // to reference the which component will be pushed\r\n    // to the navigation stack\r\n\r\n    Navigation.push(this.props.componentId, {\r\n      component: {\r\n        name: \'blog.ViewPost\',\r\n        passProps: {\r\n          text: \'Some props that we are passing\'\r\n        },\r\n        options: {\r\n          topBar: {\r\n            title: {\r\n              text: \'Post1\'\r\n            }\r\n          }\r\n        }\r\n      }\r\n    });\r\n  }\r\n\r\n\r\n\r\n  render() {\r\n    return (\r\n      <View flex center bg-blue60>\r\n        <Text onPress={this.pushViewPostScreen}>Posts List Screen</Text>\r\n      </View>\r\n    );\r\n  }\r\n}\r\n\r\nexport default PostsList;
Run Code Online (Sandbox Code Playgroud)\r\n
\r\n
\r\n

\n\n

官方文档中,负责推送、弹出和更改导航的 Screen API 似乎可以通过Navigation始终期望 aprops.componentId获取组件引用的模块进行访问。

\n

  • 谢谢,但我的问题仍然存在。为什么新的 API 需要它?它的目的是什么?从上面我只能得到的是它是需要的,但不是原因。 (7认同)