React Native-How do I start a TextInput with a value read from AsyncStorage when loading/rendering the screen first time in functional component?

Viewed 177

I've tried several ways without success with the useEffect hook. And I haven't found the solution anywhere. The purpose is to read the email saved in AsyncStorage and automatically populate the TextInput when loading the login screen (like as defaultvalue). Could anyone help?

1 Answers

I used this way: my handling:

const [value, setValue] = useState();
const demo = async () => {
try {
  const data = await AsyncStorage.getItem('_token');
  setValue(data);
} catch (e) {
  console.log(e);
}
};
useEffect(() => {
  demo();
}, []);

display part:

<TextInput
    style={{width: 100, height: 50, marginTop: 100, backgroundColor: 'red'}}
    value={value}
    onChangeText={setValue}
  />
Related