I simplified the problem I had in my application as follows. Even if I enter a new word in the TextInput
, when I go back in the page it says "initial"
. Why doesn’t it write new value?
Actually, what I want to do here is to send the last entered text to redux as soon as I press the phone’s back button(hardwareBackPress). But it always sends the initial value. How can I solve this problem?
import { TextInput } from 'react-native'
import React, { useEffect, useState } from 'react'
export default function ChangeModel({ navigation }) {
const [text, setText] = useState("initial")
const onPressSave = () => {
console.log(text)
}
useEffect(() => {
return () => onPressSave()
}, [])
return (
<TextInput
value={text}
onChangeText={setText}
style={{ backgroundColor: "grey" }}
/>
)
}
2
Answers
you’re not set anything that makes the value change, try to set the state with new value:
if you handle with
redux
you should import theaction
first anddispatch
it after thatTo get entered data you have to write a button component like this
When you click the button on the back of the phone, the state is default