Home > Software engineering >  How to set to true / false based on react props
How to set to true / false based on react props

Time:11-24

const [effective_date_status, setEffectiveDateStatus] = React.useState(false)

I'm sending props value from parent to child. I want to set effective_date_status to false if props value is 0 & I want to set to true if props value is 1.

Can anyone help me with this issue.

CodePudding user response:

You can do as:

const [effective_date_status, setEffectiveDateStatus] = React.useState(
  !!props.value
);

or

const [effective_date_status, setEffectiveDateStatus] = React.useState(
  props.value === 0 ? false : true
);

or Lazy Initialisation

const [effective_date_status, setEffectiveDateStatus] = React.useState(() =>
  props.value === 0 ? false : true
);

CodePudding user response:

first, import use effect hook and then try this

useEffect(()=>{
if(props.value === 1)
setEffectiveDateStatus(true);
else if(props.value === 0)
setEffectiveDateStatus(false);
},[props.value])
  • Related