【问题标题】:Sharing state with react navigation与反应导航共享状态
【发布时间】:2022-01-10 08:10:32
【问题描述】:

在这种情况下,从父组件和朋友组件之间传递用户名和更改 setUsername 的“智能/假定”方式是什么?

据我了解,当我使用反应导航(?)时,它与 App.js 的共享功能不同,因此假设该应用程序仅包含 3 个共享和设置状态的组件 - 我是否必须使用上下文/为此而减少?谢谢

App.js

const [username, setUsername] = useState('Testname'); 

        <Stack.Screen name="Parent" component={Parent}/>
        <Stack.Screen name="Friend" component={Friend}/>

Parent.js

export const Parent = ({navigation}) => {
return(
            <View style={styles.container}>
                <Text>Hello user:</Text>
                <Button title="go to friend" onPress={()=>navigation.navigate("Friend")}/>
            </View>

Friend.js

export const Friend = () => {
return(
    <TouchableOpacity>
        <Text>This is the Friend page</Text>
   <TouchableOpacity>

【问题讨论】:

  • This 会回答您的问题吗?
  • 啊,好吧,所以我想上下文是要走的路,谢谢!

标签: reactjs react-native expo


【解决方案1】:

据我了解,最好的方法是使用 Context/Hooks。

【讨论】:

    猜你喜欢
    • 2020-07-14
    • 1970-01-01
    • 1970-01-01
    • 2018-07-11
    • 1970-01-01
    • 1970-01-01
    • 2019-01-30
    • 1970-01-01
    • 2017-10-28
    相关资源
    最近更新 更多