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