简体   繁体   English

如何从一个组件传递 state 以设置另一个 state?

[英]How can I pass state from one component to set another state?

I'm currently developing an app with a modal which contains a calendar.我目前正在开发一个带有包含日历的模态的应用程序。 The user can essentially select a new date, which will update the state. However, it's not currently working as expected and I need to be able to pass the newDate state to another component to be able to update another state with the value.用户基本上可以 select 一个新日期,这将更新 state。但是,它目前没有按预期工作,我需要能够将 newDate state 传递给另一个组件,以便能够使用该值更新另一个 state。

When the user clicks the 'Change Date' button, it should update setDeliveryDate (modal.js) with newDate (from modalbutton.js) ie setDeliveryDate(newDate).当用户单击“更改日期”按钮时,它应该使用新日期(来自 modalbutton.js)更新 setDeliveryDate(modal.js),即 setDeliveryDate(newDate)。

// modal.js // 模态.js

interface ModalProps {
  closeModal: () => void;
  confirmDateChange: (newDate) => void;
}

const Modal: FunctionComponent<ModalProps> = ({
  closeModal,
  confirmDateChange,
}) => {
  const [newDate, setNewDate] = useState(new Date());

  return (
    <StyledModal>
      <DatePicker onChange={setNewDate} value={newDate} />
      <button onClick={closeModal}>Cancel, Don't Change</button>
      <button onClick={() => confirmDateChange(newDate)}>Change Date</button>
    </StyledModal>
  );
};

export default Modal;

// modalbutton.js // 模态按钮.js

const ModalButton: FunctionComponent = () => {
  const [openModal, setOpenModal] = useState(false);
  const [deliveryDate, setDeliveryDate] = useState(0);

  const handleOpenModal = () => {
    setOpenModal(true);
  };

  const confirmDateChange = (newDate) => {
    setDeliveryDate(newDate);
    setOpenModal(false);
  };

  const closeModal = () => {
    setOpenModal(false);
  };

  return (
    <StyledContainer>
      <TransitionGroup component={null}>
        {openModal && (
          <CSSTransition classNames="modal-container" timeout={300}>
            <Modal
              closeModal={closeModal}
              confirmDateChange={confirmDateChange}
            />
          </CSSTransition>
        )}
      </TransitionGroup>
    </StyledContainer>
  );
};

export default ModalButton;

It looks like you're passing the state update function correctly, but that function needs a value called newDate :看起来您正在正确传递 state 更新 function,但是 function 需要一个名为newDate的值:

const confirmDateChange = () => {
    setDeliveryDate(newDate);
    setOpenModal(false);
};

That value doesn't exist anywhere that function can see.该值不存在于 function 可以看到的任何地方。 Add it as a parameter to the function:将其作为参数添加到 function:

const confirmDateChange = (newDate) => {
    setDeliveryDate(newDate);
    setOpenModal(false);
};

After that, it looks like you're intending to pass the newDate value from the Modal component, but this isn't how you do that:之后,您似乎打算Modal组件传递newDate值,但这不是您这样做的方式:

<button onClick={confirmDateChange} newDate={newDate}>

What you're doing here is passing it as a property to <button> , which has no such property and won't do anything with it.你在这里所做的是将它作为属性传递给<button> ,它没有这样的属性并且不会对它做任何事情。 To pass it as a function argument to confirmDateChange , wrap that call in a function and add the argument:要将其作为 function 参数传递给confirmDateChange ,将该调用包装在 function 中并添加参数:

<button onClick={() => confirmDateChange(newDate)}>

David is correct, you are trying to pass value to a function without declaring it as a parameter first, so setDeliveryDate(newDate) would always just return the original value passed to newDate here const [newDate, setNewDate] = useState(new Date()); David 是正确的,您试图将值传递给 function 而不先将其声明为参数,因此setDeliveryDate(newDate)将始终只返回传递给 newDate 的原始值,此处const [newDate, setNewDate] = useState(new Date());

You can also look into React Context if you want to keep passing around data from one component to another.如果您想继续将数据从一个组件传递到另一个组件,您还可以查看 React Context。 I found it quite helpful to keep the logic in one file and have the components work with it, rather than having the components work with each other directly我发现将逻辑保存在一个文件中并让组件与其一起工作非常有帮助,而不是让组件直接相互工作

暂无
暂无

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

相关问题 如何将 state 从一个组件传递到另一个组件,如何修改 state? - How can I pass the state from one component to another and how can I modify the state? 如何在反应 class 组件中设置另一个 state 以将数据从初始 state 传递到 - How can I set another state to pass data to from the initial state in a react class component React / Redux:如何将此状态从一个组件传递到另一个文件? - React/Redux: How can I pass this state from one component to another file? 如何在 react 中重定向到另一个组件并传递我在前一个组件中设置的 state? - How can I redirect to another component in react and pass the state that I set in the previous component? 在 ReactJs 中将 state 从一个组件传递到另一个组件 - Pass state from one component to another in ReactJs 如何将 state 变量的值从一个组件传递到另一个组件? - How to pass value of state variable from one component to another? 如何在React js中将状态从一个组件传递到另一个组件? - How to pass state from one component to another in React js? 我如何通过链接传递其状态在一个组件中设置的对象的属性,并在 React 中的另一个组件中呈现它们 - How do i pass the properties of an object whose state is set in one component through a link and render them in another component in React 如何将值从状态传递到另一个组件 - How to pass values from a state to another component 如何将状态从这个子组件传递给父组件? - How can I pass the state from this child to parent component?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM