Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/477.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/reactjs/27.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript React Native:如何在React导航中传递道具_Javascript_Reactjs_React Native_React Navigation - Fatal编程技术网

Javascript React Native:如何在React导航中传递道具

Javascript React Native:如何在React导航中传递道具,javascript,reactjs,react-native,react-navigation,Javascript,Reactjs,React Native,React Navigation,在我的React本机应用程序中,我正在使用并且有一个类组件使用: 但要注意的是,我想尝试使用上下文。理想情况下,我将能够推到堆栈并将WebViewScreen指向任何url: this.props.navigation.push('Viewer', { route: 'https://www.example.com' }) 添加提供程序并获取组件中的值 const MyContext = React.createContext(); <MyContext.Provider va

在我的React本机应用程序中,我正在使用并且有一个类组件使用:

但要注意的是,我想尝试使用上下文。理想情况下,我将能够推到堆栈并将
WebViewScreen
指向任何url:

this.props.navigation.push('Viewer', {
    route: 'https://www.example.com'
})

添加提供程序并获取组件中的值

const MyContext = React.createContext();

<MyContext.Provider value={{url: '/somethig'}}>
  <Stack.Navigator>
    <Stack.Screen name="Viewer" component={WebViewScreen} />
  </Stack.Navigator>
</MyContext.Provider>

如果您只关心Web视图所在屏幕上导航操作的值更改,您只需通过
路径
道具访问导航时传递的数据即可:

const route = this.props.route.params?.route ?? 'default url';

<WebView source={{ uri: route }} />
const route=this.props.route.params?.route默认url';

资料来源:

实际上,我把它从一个功能组件改写成了一个类组件,认为我需要这样做才能访问道具、状态和生命周期等。。。我知道有一些解决方法,但我也遇到了问题,因为上下文总是说组件中未定义
MyContext
。您必须导入
MyContext
,才能在另一个组件中使用它谢谢,这非常有效!它没有使用上下文,但实际上可能是一种方式。
this.props.navigation.push('Viewer', {
    route: 'https://www.example.com'
})
const MyContext = React.createContext();

<MyContext.Provider value={{url: '/somethig'}}>
  <Stack.Navigator>
    <Stack.Screen name="Viewer" component={WebViewScreen} />
  </Stack.Navigator>
</MyContext.Provider>
const value = useContext(MyContext);
const route = this.props.route.params?.route ?? 'default url';

<WebView source={{ uri: route }} />