简体   繁体   English

传递反应状态

[英]Passing React State

I have a modal that I want to appear on a button click.我有一个模式,我想在单击按钮时出现。 This works fine, however, in order for it to be positioned correctly, I need to move the modal component to be rendered in a parent component.这很好用,但是,为了正确定位它,我需要移动模态组件以在父组件中呈现。 I'm unsure how to do this as I'm not sure how I would pass the state down to the component where the button click is created.我不确定如何执行此操作,因为我不确定如何将状态传递给创建按钮单击的组件。 I'm also unsure of whether this is about passing a state or using a button onClick outside the component the state is defined in.我也不确定这是关于传递状态还是在定义状态的组件之外使用 onClick 按钮。

My code works and does what I want, but, I need <DeleteWarehouseModal show={show} /> to be in the parent component below.我的代码可以正常工作并执行我想要的操作,但是,我需要<DeleteWarehouseModal show={show} />位于下面的父组件中。 How can I do this?我怎样才能做到这一点?

The parent component where I want to render my modal:我要呈现模态的父组件:

function WarehouseComponent(props) {
  return (
    <section>
      <div>
          <WarehouseListItems warehouseData={props.warehouseData} />
          //Modal component should go here
      </div>
    </section>
  );
}

The component (WarehouseListItems) where the modal is currently being rendered:当前正在呈现模式的组件(WarehouseListItems):

function WarehouseListItem(props) {
  const [show, setShow] = useState(false);

  return (
     <>
//some code necessary to the project, but, irrelevant to this issue

        <Link>
          <div onClick={() => setShow(true)}></div>
        </Link>
      <DeleteWarehouseModal show={show} />
     </>
  );
}

The modal:模态:

const DeleteWarehouseModal = (props) => {
  if (!props.show) {
    return null;
  }
  return (
    //some elements here
  );
};

Yes, you can move the state and it's handler in WarehouseComponent component and pass the handler down to child component, which can change the state in parent component.是的,您可以在WarehouseComponent组件中移动状态及其处理程序并将处理程序向下传递给子组件,这可以更改父组件中的状态。

WarehouseComponent :- WarehouseComponent :-

function WarehouseComponent(props) {
  const [show, setShow] = useState(false);

  const toggleShow = () => {
    setShow(state => !state);
  }
  return (
    <section>
      <div>
          <WarehouseListItems 
            warehouseData={props.warehouseData}
            toggleShow={toggleShow}
          />
          <DeleteWarehouseModal show={show} />
      </div>
    </section>
  );
}

WarehouseListItems : - WarehouseListItems :-

function WarehouseListItem(props) {
  const {toggleShow} = props;

  return (
     <>
//some code necessary to the project, but, irrelevant to this issue

        <Link>
          <div onClick={() => toggleShow()}></div>
        </Link>
      
     </>
  );
}

I'm not sure if I'm getting the question but I think you need to create a Portal for the proper render of the Modal我不确定我是否收到了这个问题,但我认为您需要创建一个门户以正确渲染模态

enter link description here在此处输入链接描述

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM