简体   繁体   English

如何根据反应道具设置为真/假

[英]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 .如果 props 值为0 ,我想将Effective_date_status设置为false ,如果 props 值为1 ,我想设置为true

Can anyone help me with this issue.谁能帮我解决这个问题。

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 InitialisationLazy Initialisation

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

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])

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何在对象数组中设置基于True和False的特定键? - How to set True and False based particular key in object Array? 如何根据字段值设置标记true / false? - How to set the flag true/false based on field values? 如何根据 props 在 React 中设置初始 useState 值 - How to set initial useState value in React based on props 如何基于HTML函数返回true / false并设置禁用值? - How do I return true/false based on the function to HTML and set the disabled value? 如何使用三元运算符根据使用 javascript 的值将变量设置为真或假? - How to use ternary operator to set a variable to true or false based on value using javascript? 反应如何切换 mui ListItemIcon true/false - react how to toggle mui ListItemIcon true/false 如何使用javascript中的三元运算符根据长度将变量设置为true或false? - How to set the variable to true or false based on the length using ternary operator in javascript? 如何在点击时将功能设置为true / false? - How to set function to true/false on click? 如何使用.val()将var设置为true或false - How to set a var to true or false using .val() 每当 React 中的某个属性设置为 true 时,我如何将 object 属性切换为 false? - How would I toggle object properties to false, whenever one of the properties is set to true in React?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM