【发布时间】:2020-03-28 16:39:50
【问题描述】:
所以我的屏幕代码看起来像这样。我有一个 useState amount 在重新渲染时不断重置为 initialValue。我有大约 3 个 useEffects。其中之一是在这里的示例代码中,我从 db 获取金额并使用 setAmount 设置它。
const ConfirmPaymentDialog = props => {
const getInitialVal = () => {
console.log('Initial Amount: 0');
return conversions.numberToBigNumber(0);
};
const [amount, setAmount] = useState<BigNumber>(getInitialVal());
useEffect(() => {
getAmountToPay();
},[]);
const getAmountToPay = async () => {
const amount = await myDbModule.getAmount(customer.id);
console.log('DB Amount: ' + amount?.toString());
setAmount(amount || conversions.numberToBigNumber(0));
};
useEffect(() => {
...
}, [isInputTextFocused])
console.log('Rendering');
return (<View>...</View>);
}
当我打开这个屏幕时。我得到以下控制台日志
LOG Initial Amount: 0
LOG Rendering
LOG DB Amount: 500
LOG Initial Amount: 0
LOG Rendering
LOG Initial Amount: 0
LOG Rendering
LOG Initial Amount: 0
LOG Rendering
LOG Initial Amount: 0
LOG Rendering
LOG Initial Amount: 0
LOG Rendering
如何阻止我的 useState 在重新渲染时重置回默认值
【问题讨论】:
标签: react-native react-native-android react-hooks