简体   繁体   English

将信息从子组件传递给父组件

[英]Passing information from child component to parent

This is a follow up question to this question:这是此问题的后续问题:

Call child method from parent 从父调用子方法

I am using React > 16.8 with function components and hooks.我正在使用带有 function 组件和挂钩的 React > 16.8。

I got a parent component which manages a component to display items and add a new item.我有一个父组件,它管理一个组件来显示项目并添加一个新项目。 The list of items is at the parent component.项目列表位于父组件中。 The way the items are added are by a "+" button which opens a new modal window (which is a component of its own), and inside there's a form that the user can insert the details of the new items.添加项目的方式是通过“+”按钮打开一个新的模式 window(它是它自己的组件),并且在里面有一个表单,用户可以插入新项目的详细信息。

const registerFormRef = useRef();
<Modal
    isOpen={isFormOpen}
    onCancel={() => setIsFormOpen(false)}
    onSubmit={() => { registerFormRef.current.onSubmitForm(); setIsFormOpen(false) }}
    titleText="Register Tenant">
      <AddItem onAddItem={AddNewItem} ref={registerFormRef}></RegisterTenant>
</Modal>

The AddNewItem is a callback which adds the new item to the list. AddNewItem是一个回调,它将新项目添加到列表中。 The modal has an "OK" button which serves as a submit button.该模式有一个“确定”按钮,用作提交按钮。 It belongs to the parent modal component, not the AddItem child.它属于父模态组件,而不是AddItem子组件。

The method in the child component:子组件中的方法:

useImperativeHandle(ref, () => (
{
    onSubmitForm()
    {
        setIsLoading(true);
        const newItem = {
            name: formSettings["name"].value,
            description: formSettings["description"].value,
            area: "",
            category: ""
        }
        props.onAddItem(newItem);
        setIsLoading(false);
    }

})); 

I had an issue of getting the information from the child component which holds the form to the parent component, since the submit button as I said, belongs to the modal, I had to somehow call the callback from inside the child form.我有一个从持有表单的子组件获取信息到父组件的问题,因为正如我所说的提交按钮属于模式,我不得不以某种方式从子表单内部调用回调。 I have used the accepted answer in the linked question above.我在上面的链接问题中使用了公认的答案。 It works, but the comment says it's not a good practice passing information like that.它有效,但评论说这样传递信息不是一个好习惯。 Is there another way of passing the information from the child form to the parent component?是否有另一种将信息从子窗体传递到父组件的方法?

The correct way is to store the form data in the parent ie the component rendering the modal.正确的方法是将表单数据存储在父级中,即呈现模态的组件中。 To do that you could define a state and provide an onChange handler to it.为此,您可以定义 state 并为其提供 onChange 处理程序。 Once you do that on any change in input the AddItem component must notify its parent by calling the onChange method一旦您对输入的任何更改执行此操作,AddItem 组件必须通过调用 onChange 方法通知其父级

const App = () => {
   const [data, setData] = useState();
   const handleChange=(newData) => {
       setData(newData);
   }
   const onSubmit = () => {
      // use data to do whatever you want with the formData
      console.log(data); 
      setIsFormOpen(false) 
   }
   ...
   return (
        <Modal
            isOpen={isFormOpen}
            onCancel={() => setIsFormOpen(false)}
            onSubmit={onSubmit}
            titleText="Register Tenant">
              <AddItem onAddItem={AddNewItem} handleChange={handleChange} ref={registerFormRef}></RegisterTenant>
        </Modal>
   )
}

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

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