在检查propstype时如何设置Usestate

发布于 2025-01-28 13:36:10 字数 1158 浏览 5 评论 0原文

您好,我正在尝试设置Usestate,但它使整个代码崩溃。在这里,我将PropStype从父组件发送到子组件。因此,当检查ProPstype条件/TRUE时,我将尝试设置Usestate,而不是设置它会崩溃。请在下面检查我尝试的内容,在这里我删除了不需要的代码,否则对读者来说太大了,他们可能会从疑问中失去焦点。谁能为此建议我尝试很多新的解决方案,然后来这里以寻求适当的解决方案。

parent组件(BillComponent)

const BillComponent = () => {
return(  
{/* Add Bill form */}
        <Drawer
          title={"Add Bill"}
          placement="right"
          onClose={onClose}
          visible={visible}
          className="ant-drawer-half"
        >
          <AddBill parentCallback={handleCallback} type="add" />          // <---This AddBill is child component and here I'm passing props type as add
        </Drawer>
    </>
    </>
  );
};
export default BillComponent;

儿童组件(addBill)

const AddBill = (props) => {
const [checked, setChecked] = useState(false);

if(props.type === "add"){                       
    setChecked(false);          //<---This is where main code is crashing. when I set useState 
  }else{
    setChecked(true);
  }

return( 
// divs of project
 );
};
export default AddBill;

hello I'm trying to set useState but it crashing the whole code. here I'm sending propstype from parent component to child component. so when propstype condition is checked/true then I'm trying to set useState but instead of setting it crashes. please checked below what I tried, Here I've removed unwanted code otherwise it'll too big for readers and they might lost the focus from question. can anyone suggest me any new solution for this I've tried a lot of and then came here for the proper solution.

parent component (BillComponent)

const BillComponent = () => {
return(  
{/* Add Bill form */}
        <Drawer
          title={"Add Bill"}
          placement="right"
          onClose={onClose}
          visible={visible}
          className="ant-drawer-half"
        >
          <AddBill parentCallback={handleCallback} type="add" />          // <---This AddBill is child component and here I'm passing props type as add
        </Drawer>
    </>
    </>
  );
};
export default BillComponent;

child component (AddBill)

const AddBill = (props) => {
const [checked, setChecked] = useState(false);

if(props.type === "add"){                       
    setChecked(false);          //<---This is where main code is crashing. when I set useState 
  }else{
    setChecked(true);
  }

return( 
// divs of project
 );
};
export default AddBill;

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

情丝乱 2025-02-04 13:36:10

不确定这是您的情况,因为您没有显示错误。错误可能是与“过多的重新订阅者”有关的。

在组件的“根”中使用这些反应钩是一种不好的做法。在您的特殊情况下,我会做以下操作:

const [checked, setChecked] = useState(false);

useEffect(() => {
  if (props.type === "add") {
    setChecked(false);
  } else {
    setChecked(true);
  }
}, [props.type]);

Not sure if that's your case because you didn't show the error. Probably the error will be something related to "Too many re-renders".

It's a bad practice to use those React hooks in the "root" of the component. In your particular case, I'd do the following:

const [checked, setChecked] = useState(false);

useEffect(() => {
  if (props.type === "add") {
    setChecked(false);
  } else {
    setChecked(true);
  }
}, [props.type]);
星星的軌跡 2025-02-04 13:36:10

请分享您遇到的错误。
另外为什么不只是使用:
const [已检查,setchecked] = usestate(props.type ==='add');

Please share the error you're getting.
Also why not just use:
const [checked, setChecked] = useState(props.type === 'add');

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文