[英]Error initialising react hook with a function call
Why this code doesn't work:为什么此代码不起作用:
let [kgFormValues, setKgFormValues] = useState<KgFormValues>(getKgFormValues());
Error: ReferenceError: Cannot access 'kgFormValues' before initialization错误:ReferenceError:初始化前无法访问“kgFormValues”
But when removing the parentheses works:但是当删除括号有效时:
let [kgFormValues, setKgFormValues] = useState<KgFormValues>(getKgFormValues);
function getKgFormValues() {
return {
...
};
}
In order to get this to work, you must call the function after it's declared.为了使它工作,您必须在声明后调用 function。
For example:例如:
function getKgFormValues() {
return {
...
};
}
// now you can use it, as such:
let [kgFormValues, setKgFormValues] = useState<KgFormValues>(getKgFormValues);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.