PARENT
import Child from '../Child';
const Parent = () => {
const [inputValue, setInputValue] = useState('');
const handleChild = (value) => {
setInputValue(value);
console.log('VALUE:', value);
console.log('STATE:', inputValue);
};
console.log(inputValue); //shows the same as VALUE
return (
<View>
<Child passInputValue={handleChild} />
</View>
);
};
Child
const Child = ({passInputValue}) => {
const [input, setInput] = useState('');
return (
<View>
<TextInput value={input} onChangeText={(text) => setInput(text)} />
<TouchableOpacity onPress={() => passInputValue(input)}>
<Text>SEND DATA TO PARENT</Text>
</TouchableOpacity>
</View>
);
};
When I fill the input and then press the button, the console shows me in VALUE: the current value of the input and in STATE the previous value of input
"CLICK 1"
VALUE: 'WHATEVER'
STATE: ''
"CLICK 2"
VALUE: 'WHATEVER'
STATE: 'WHATEVER'
what I need is that when I click, execute realm and save the value of inputValue, but it always saves the previous value
I would appreciate any help :)