英文:
Passing data in callback function from Modal
问题
你想从"StudentForm"传递数据给"ModaltoBlock"函数。你可以在"StudentForm"组件中调用"props.modaltoblock",这将触发"ModaltoBlock"函数。在"StudentForm"组件的Form标签中,你可以使用类似这样的方法:
<Form onSubmit={() => props.modaltoblock()}>
这将在表单提交时调用"ModaltoBlock"函数,并传递数据给它。
英文:
I am new to wonderland of react and exploring using Mantine library.
I have a modal as below which has a child Component "StudentForm". I wish to take input from StudentForm and pass that value to ParentComponent.
ParentComonent:
const modaltoblock = () => {
console.log("Hit");
}
const openModal = () => modals.openConfirmModal({
title: 'Input Data',
children: (
<StudentForm modaltoblock={modaltoblock}></StudentForm>
),
labels: { confirm: 'Confirm', cancel: 'Cancel'},
onCancel: () => console.log('Cancel'),
onConfirm: () => (
//handleIncrement(),
notifications.show({
title: 'Input Submitted',
message: 'Trial Complete',
autoClose: 1000,
withCloseButton: true
})),
});
export default function StudentForm(props) {
return (
<Container>
<Title>Input Data for : </Title>
<Form onSubmit={props.modaltoblock()}>
//input field
</Form>
</Container>
);
}
How do I pass on data to ModaltoBlock function from StudentForm?
答案1
得分: 1
请使用箭头函数
const StudentForm = ({ modaltoblock }) => {
return (
<Container>
<Title>输入数据 : </Title>
<Form onSubmit={() => modaltoblock('HI')}/>
</Container>
);
}
export default StudentForm;
到目前为止,我们发送了单词'HI'
然后现在我们可以将该值作为参数传递给函数
const modaltoblock = (value) => {
console.log("查看这里", value);
}
英文:
please use arrow functions
const StudentForm=({modaltoblock})=> {
return (
<Container>
<Title>Input Data for : </Title>
<Form onSubmit={modaltoblock('HI')}/>
</Container>
);
}
export default StudentForm;
so far we are sending the word 'HI'
then now we can get that value at the function as a parameter
const modaltoblock = (value) => {
console.log("SEE HERE", value);
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论