从模态窗口中传递回调函数中的数据

huangapple go评论67阅读模式
英文:

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 = () =&gt; {
    console.log(&quot;Hit&quot;);
  }

  const openModal = () =&gt; modals.openConfirmModal({
    title: &#39;Input Data&#39;,
    children: (
        &lt;StudentForm modaltoblock={modaltoblock}&gt;&lt;/StudentForm&gt;
    ),
    labels: { confirm: &#39;Confirm&#39;, cancel: &#39;Cancel&#39;},
    onCancel: () =&gt; console.log(&#39;Cancel&#39;),
    
    onConfirm: () =&gt; (
      //handleIncrement(),
      notifications.show({
      title: &#39;Input Submitted&#39;,
      message: &#39;Trial Complete&#39;,
      autoClose: 1000,
      withCloseButton: true
    })),
  });
export default function StudentForm(props) {
  return (
      
      &lt;Container&gt; 
             
             &lt;Title&gt;Input Data for :  &lt;/Title&gt;
             
             &lt;Form onSubmit={props.modaltoblock()}&gt;

                //input field
                &lt;/Form&gt;
&lt;/Container&gt;
);
}

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})=&gt; {
  return (
      &lt;Container&gt; 
             &lt;Title&gt;Input Data for :  &lt;/Title&gt;
             &lt;Form onSubmit={modaltoblock(&#39;HI&#39;)}/&gt;
      &lt;/Container&gt;
 );
}
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) =&gt; {
    console.log(&quot;SEE HERE&quot;, value);
}

huangapple
  • 本文由 发表于 2023年3月31日 03:28:59
  • 转载请务必保留本文链接:https://go.coder-hub.com/75892255.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定