这里我使用useState将状态'unligible'设置为false作为开始
const [state,setState] = React.useState({
uneligible: false,
})
然后在这里我编写一个将状态设置为true的函数,这是我收到错误的地方:
function setEligibility(){state.uneligible==='true'}
这是将其设置为true的条件(如果user <18状态的age应该为true):
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));
console.log(age);
if(age<18){setEligibility();}
如果状态成功设置为true(如果用户的年龄<18岁),我将以此方式显示警报组件:
<Grid item xs={8} style={{marginTop:"10px"}}>{state.uneligible&&<Alert variant="filled" severity="error">
This is an error alert — check it out!
</Alert>}</Grid>
我需要帮助来修复错误并使警报组件出现
您不需要做一个改变状态的函数
const [eligible,setEligible] = React.useState(
false // default value
)
更新状态
setEligible(true)
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句