【发布时间】:2021-01-23 22:11:50
【问题描述】:
家长
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>
);
};
孩子
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>
);
};
当我填写输入然后按下按钮时,控制台会在 VALUE 中显示我:输入的当前值并在 STATE 中显示输入的前一个值
“点击 1”
价值: “随便”
状态: ''
“点击 2”
价值: “随便”
状态: “随便”
我需要的是,当我点击时,执行realm并保存inputValue的值,但它总是保存之前的值
我会很感激任何帮助:)
【问题讨论】:
标签: javascript react-native react-hooks state