我再次詢問有關 ReactNative 的問題:我有以下 App.js:
export default function App() {
function Tabs() {
return <TabBar />;
}
const styles = StyleSheet.create({
backButton: {
color: global.GUI.ORANGE,
},
});
return (
<NavigationContainer>
<Stack.Navigator>
<Stack.Screen
name="Tabs"
component={Tabs}
..
/>
<Stack.Screen
name="Impostazioni"
component={Settings}
...
/>
<Stack.Screen
name="Registrati"
component={Signup}
...
/>
<Stack.Screen
name="Login"
component={Login}
...
/>
</Stack.Navigator>
</NavigationContainer>
);
}
我想將一些在 App.js 中宣告的值傳遞給每個組件,但要執行以下操作:
<Stack.Screen
name="Impostazioni"
component={Settings}
currentUser={"name":"test"}
/>
回傳未定義:
import React from 'react';
import {View,Text} from 'react-native';
const Settings = (props) => {
--> console.log(props.currentUser) // here is undefined
return (
<View >
<Text>This is Settings!</Text>
</View>
);
};
export default Settings
那么,我怎樣才能正確地將 App.js 的 props 傳遞給所有其他組件呢?
謝謝
uj5u.com熱心網友回復:
您必須使用 React Context(推薦)或將道具傳遞給您的組件。
https://wix.github.io/react-native-navigation/docs/third-party-react-context/
其他方式:
<Stack.Navigator initialRouteName="LoginScreen" headerMode="none">
<Stack.Screen name="LoginScreen" component={LoginScreen} initialParams={{'key':'value'}} />
<Stack.Screen name="CodeVerificationScreen" component={CodeVerificationScreen} initialParams={{'key':'value'}} />
</Stack.Navigator>
您可以在 login.js 中接收初始引數
console.log(this.props.route.params.key)
另一種方法:
將道具傳遞給組件 - 檔案鏈接:(請檢查您的導航版本) https://reactnavigation.org/docs/hello-react-navigation/#passing-additional-props
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/419640.html
標籤:
上一篇:javascript函式報告問題缺少分號,錯誤是什么?
下一篇:替換作為方法引數接收的陣列中的值
