Home > database >  Material UI checkbox toggle value based off input from object
Material UI checkbox toggle value based off input from object

Time:05-24

I have an unchecked checkbox. I am trying to change the value of it based off of data from an object. The object is from an SQL select boolean column 'T' or 'F'. If the value is 'T' then the box would be checked vise versa. I tried using a useState() that viewed the value but that didn't work.

  const [checkBoxState, setCheckBoxState] = React.useState(false);
  //check to see if values are 't' or 'f' to change them to vaiable formats
  function handleCheckState(databaseCondition) {
    if (databaseCondition == "T") {
      setCheckBoxState = true;
    }
    console.log(checkBoxState);
    return checkBoxState;
  }

This is the useState() I tried using.

<Checkbox checked={handleCheckState(data["validcycle"])} />

Here is the checkbox I want to toggle on/off based off that sql column.

CodePudding user response:

Friend, you forgot to enclose hook in brackets, example : setState(value)

    const [checkBoxState, setCheckBoxState] = React.useState(false)

    function handleCheckState(databaseCondition) {
        if (databaseCondition == 'T') setCheckBoxState(true)
        else setCheckBoxState(false)
        return checkBoxState
    }

CodePudding user response:

you must change the state using:

setCheckBoxState(true);

instead of:

setCheckBoxState = true;

the better approach is simply like this:

<Checkbox checked={databaseCondition === "T"} />
  • Related