[英]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 Initialisation
或
Lazy 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.