score:1

Accepted answer

you don't need to make a function to change state

const [eligible,seteligible] = react.usestate(
false  // default value
)

to update state state

seteligible(true)

score:0

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. if you want to keep with the object you've got, you'd do:

function seteligibility() { 
   setstate({uneligible: true});
}

score:0

the state.uneligible==='true' is a condition, instead of assignment.

you should change the below line:

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

to

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

score:0

you are declaring the state variable name as state which is not a good practice and might throw a keyword error as well. instead, use the variable name as eligible.

define the state like this:

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. 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;
}

Related Query