简体   繁体   English

如何将 useState 挂钩的 state 设置为 true

[英]how to set the state of useState hook to true

here I am using useState to set the state 'uneligible' to false as a start在这里,我使用 useState 将 state '不合格' 设置为 false 作为开始

    const [state,setState] = React.useState({
   
    uneligible: false,
  
  })

then here I am writing a function to set the state to true and here's where I am getting the error:然后在这里我正在编写 function 以将 state 设置为 true,这就是我收到错误的地方:

 function setEligibility(){state.uneligible==='true'}

and that's the error:这就是错误: 在此处输入图像描述

and here's the condition for setting it to true(if age of user< 18 state should be true):这是将其设置为 true 的条件(如果用户年龄 < 18 state 应该为 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();}

and that's how I will call it to show the alert component if the state is successfully set to true(if user's age <18):如果 state 成功设置为 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>
                   

I need help to fix the error and make the alert component appear我需要帮助来修复错误并使警报组件出现

You don't need to make a function to change state您无需制作 function 即可更改 state

const [eligible,setEligible] = React.useState(
false  // default value
)

To Update State state更新 State state

setEligible(true)

I get the impression that you read two different react approaches and mixed them in your code: the old class-based (without hooks but with an explicit state), and the hook based.我的印象是您阅读了两种不同的反应方法并将它们混合到您的代码中:旧的基于类(没有钩子但具有显式状态)和基于钩子。 With hooks you don't really have a state, and as Fahad pointed out you can just set individual (state-)variables directly.使用钩子,您实际上并没有 state,正如 Fahad 指出的那样,您可以直接设置单个(状态)变量。 If you want to keep with the object you've got, you'd do:如果您想保留现有的 object,您可以:

function setEligibility() { 
   setState({uneligible: true});
}

The state.uneligible==='true' is a condition, instead of assignment. state.uneligible==='true'是一个条件,而不是赋值。

You should change the below line:您应该更改以下行:

function setEligibility(){state.uneligible==='true'}

to

function setEligibility(){state.uneligible = 'true'}

You are declaring the state variable name as state which is not a good practice and might throw a keyword error as well.您将 state 变量名称声明为 state,这不是一个好的做法,也可能引发关键字错误。 Instead, use the variable name as eligible.相反,请使用符合条件的变量名称。

Define the state like this:像这样定义 state:

const [uneligible, setUneligible] = React.useState(false);

Condition for setting it would change to this:设置它的条件将变为:

var split_dob = dateOfBirth.split('-');
var dob_asdate = new Date(split_dob[0], split_dob[1], split_dob[2]);
var today = new Date();
var mili_dif = Math.abs(today.getTime() - dob_asdate.getTime());
var age = mili_dif / (1000 * 3600 * 24 * 365.25);
if (age < 18) {
  setUneligible(true);
}

If Uneligibile is set to false, you are rendering an empty Grid which does not seem to be okay.如果 Uneligibile 设置为 false,则您正在渲染一个看起来不太好的空 Grid。 Rather:相当:

if (uneligible) {
  return (
    <Grid item xs={8} style={{ marginTop: '10px' }}>
      <Alert variant="filled" severity="error">
        This is an error alert — check it out!
      </Alert>
    </Grid>
  );
} else {
  return null;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM