Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How to set to true / false based on react props

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.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

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
);
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading