[英]I am trying to call a function from useEffect hook and it's giving me undefined
I am trying to call function 'handleSaveGeneral' from inside useEffect hook我正在尝试从 useEffect 钩子内部调用 function 'handleSaveGeneral'
const ProfilePage = (props) => {
const [uneligible, setUneligible] = React.useState(false);
useEffect(() => {
const handleSaveGeneral = (e) => {
var dateOfBirth = "2007-01-01";
var split_dob = dateOfBirth.split("-");
var month = split_dob[1];
var day = split_dob[2];
var year = split_dob[0];
var dob_asdate = new Date(year, month, day);
var today = new Date();
var mili_dif = Math.abs(today.getTime() - dob_asdate.getTime());
var age = mili_dif / (1000 * 3600 * 24 * 365.25);
setUneligible(age < 18);
};
});
return (
<div>
<Button variant="outline-primary" onClick={handleSaveGeneral}>
Save
</Button>
{uneligible && (
<Alert variant="filled" severity="error">
This is an error alert — check it out!
</Alert>
)}
</div>
);
};
the 'save' Button also doesn't render for no reason, I am trying to show Alert when we click 'save' but the function 'handleSaveGeneral' is giving me undefined in the code sandbox of the problem “保存”按钮也不会无缘无故地呈现,当我们单击“保存”时,我试图显示警报,但是 function 的“handleSaveGeneral”在问题的代码沙箱中给了我未定义
https://codesandbox.io/s/modest-field-xqf91?file=/src/App.js:257-274 https://codesandbox.io/s/modest-field-xqf91?file=/src/App.js:257-274
const
declaration is bloc-scope const
声明是块作用域
So you should define the handleSaveGeneral
function outside of useEffect
and call it inside, so that other components can get access to it所以你应该在useEffect之外定义
handleSaveGeneral
useEffect
并在里面调用它,以便其他组件可以访问它
Make sure you add an array to useEffect's dependency to avoid infinite loop确保将数组添加到 useEffect 的依赖项以避免无限循环
const handleSaveGeneral = (e) => {
var dateOfBirth = "2007-01-01";
var split_dob = dateOfBirth.split("-");
var month = split_dob[1];
var day = split_dob[2];
var year = split_dob[0];
var dob_asdate = new Date(year, month, day);
var today = new Date();
var mili_dif = Math.abs(today.getTime() - dob_asdate.getTime());
var age = mili_dif / (1000 * 3600 * 24 * 365.25);
setUneligible(age < 18);
};
useEffect(() => {
handleSaveGeneral()
},[]);
here is the whole code这是整个代码
import React, { useEffect } from "react";
import Button from "react-bootstrap/Button";
import Alert from "@material-ui/lab/Alert";
const ProfilePage = (props) => {
const [uneligible, setUneligible] = React.useState(false);
const handleSaveGeneral = (e) => {
var dateOfBirth = "2007-01-01";
var split_dob = dateOfBirth.split("-");
var month = split_dob[1];
var day = split_dob[2];
var year = split_dob[0];
var dob_asdate = new Date(year, month, day);
var today = new Date();
var mili_dif = Math.abs(today.getTime() - dob_asdate.getTime());
var age = mili_dif / (1000 * 3600 * 24 * 365.25);
setUneligible(age < 18);
};
useEffect(() => {
handleSaveGeneral()
},[]);
return (
<div>
<Button variant="outline-primary" onClick={handleSaveGeneral}>
Save
</Button>
{uneligible && (
<Alert variant="filled" severity="error">
This is an error alert — check it out!
</Alert>
)}
</div>
);
};
export default ProfilePage;
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.