Skip to content
Advertisement

Material UI checkbox toggle value based off input from object

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.

Advertisement

Answer

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
    }
User contributions licensed under: CC BY-SA
7 People found this is helpful
Advertisement