How to take a useState from one screen and use it on another screen in React Native?

Asked

Viewed 17 times

-1

Guys, I’m making an app for college in React Native and I used a useState to get the name of the user on the screen of Signin, but I don’t know how I can take this name and use in other screens in the application.

Follows the code:

const [text, setText] = useState(''); 

    return (
        <View style={styles.container}>
          <StatusBar 
            barStyle="light-content"
            backgroundColor="transparent"
            translucent
        />
          <Image
            source={IllustrationImg}
            style={styles.image}
            resizeMode={'stretch'}
          />
    
          <View style={styles.content}>
          <Text style={styles.title}>
             Bem vindo ao DowLibras {text}!
            </Text>

              
           

            <TextInput
            style={styles.input}
            onChangeText={(value) => setText(value)}
            placeholder={"Digite seu nome aqui"}
            placeholderTextColor={'white'} 
            />        `

I would really like a help to use the user name on other screens. Thank you!

  • One of the best ways you can do this today is by using the React Context API. This way you can share the state by the application, check carefully the application of the Context API as incorrectly used can generate unnecessary cycles of rendering, especially if you do not use useCallback in the functions that you will pass to other components.

No answers

Browser other questions tagged

You are not signed in. Login or sign up in order to post.