简体   繁体   English

React 16:使用钩子和功能组件时,从父级调用子级的 function

[英]React 16: Call children's function from parent when using hooks and functional component

I need to call children's function in their parent component.我需要在他们的父组件中调用孩子的 function 。 How should I do it?我该怎么做? Previous in React 15, I can use refs to call children's function.以前在 React 15 中,我可以使用 refs 来调用 children 的 function。 But have no idea how to do it with hooks and functional component.但是不知道如何使用钩子和功能组件来做到这一点。

function Child(props) {
    function validate() {
        // to validate this component
    }

    return (
        <>Some code here</>
    )
}


function Parent(props) {
    const refs = useRef([]);
    const someData = [1,2,3,4,5];

    function validateChildren() {
        refs.current.forEach(child => {
            child.current.validate(); // throw error!! can not get validate function
        });
    }

    return (
        <>
            <button onClick={validateChildren}>Validate</button>
            {
                someData.map((data, index) => {
                    return <Child ref={ins => refs.current[index] = ins} />
                })
            }
        </>
    )
}

My actual requirement is: 1. There are multiple tabs which can be added, delete base on user's behaviour 2. Click a button in the parent of tabs to trigger validate 3. All the tabs need to validate itself, show error message in their tabs and return a validate message我的实际要求是: 1. 可以添加多个选项卡,根据用户的行为删除 2. 单击选项卡父级中的按钮触发验证 3. 所有选项卡都需要自行验证,在其选项卡中显示错误消息并返回验证消息

You can use useImperativeHandle with forwardRef .您可以将useImperativeHandleforwardRef一起使用。

From the docs ,文档中,

useImperativeHandle customizes the instance value that is exposed to parent components when using ref. useImperativeHandle自定义使用 ref 时暴露给父组件的实例值。 As always, imperative code using refs should be avoided in most cases.与往常一样,在大多数情况下应避免使用 refs 的命令式代码。 useImperativeHandle should be used with forwardRef useImperativeHandle应该与forwardRef一起使用

const Child = React.forwardRef((props,ref) => {

    //validate will be available to parent component using ref
    useImperativeHandle(ref, () => ({
      validate() {
        // to validate this component
        console.log("I'm clicked");
      }
    }));

    return (
        <>Some code here</>
    )
})

In the parent compoennt you can access child function as,在父组件中,您可以访问子 function 为,

function validateChildren() {
   refs.current.forEach(child => {
      child.validate(); // you can access child method here 
   });
}

Demo演示

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

相关问题 如何使用带有钩子的反应功能组件测试句柄 function 调用 - How to test a handle function call with a react functional component with hooks React hooks:动态映射的组件子组件和独立于父组件的状态 - React hooks: Dynamically mapped component children and state independent from parent 在反应16中从父级调用子级函数 - call child function from parent in react 16 Function 在 React 钩子中的功能组件内 - 性能 - Function inside functional component in React hooks - Performance React 使用功能组件更改父组件的内部功能 - React Change inner function of parent component using functional component 在子组件中调用父函数 - Call parent function in children component React 嵌套子组件回调函数无法读取父组件的状态(使用 Hooks) - React nested child component callback function cannot read parent's state (using Hooks) 使用 React Hooks,当我将 prop 从父组件传递给子组件时,子组件中的 prop 未定义 - Using React Hooks, when I pass down a prop from parent to a child component, the prop in the child component is undefined 在功能组件中使用钩子的辅助函数 - Helper function using hooks inside a functional component 如何从组件父级为组件子级 Drawer Navigator 调用 function - How to call function from component parent for component children Drawer Navigator
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM