React总结1:React Hooks 中通过父组件调用子组件中的方法
<p><span style="font-size: 18px">先上示例:</span></p><p><span style="font-size: 18px">1.子组件</span></p>
<div class="cnblogs_code">
<pre>import React, {useEffect, useImperativeHandle, forwardRef} from 'react'<span style="color: rgba(0, 0, 0, 1)">;
</span><span style="color: rgba(0, 0, 255, 1)">function</span><span style="color: rgba(0, 0, 0, 1)"> AllProjectTable(props, ref) {
useImperativeHandle(ref, () </span>=><span style="color: rgba(0, 0, 0, 1)"> ({
handleGetProjectList,
handleStopProject
}));
</span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> 获取项目列表</span>
const handleGetProjectList = () =><span style="color: rgba(0, 0, 0, 1)"> {
};
</span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> 停止项目运行</span>
const handleStopProject = project =><span style="color: rgba(0, 0, 0, 1)"> {
};
</span><span style="color: rgba(0, 0, 255, 1)">return</span><span style="color: rgba(0, 0, 0, 1)"> (
</span><>
</>
<span style="color: rgba(0, 0, 0, 1)">);
}
export </span><span style="color: rgba(0, 0, 255, 1)">default</span> forwardRef(AllProjectTable);</pre>
</div>
<p> </p>
<p><span style="font-size: 18px">2.父组件</span></p>
<div class="cnblogs_code">
<pre>import React, {useRef} from 'react'<span style="color: rgba(0, 0, 0, 1)">;
</span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> 引用子组件</span>
import AllProjectTable from './components/AllProjectTable'<span style="color: rgba(0, 0, 0, 1)">;
</span><span style="color: rgba(0, 0, 255, 1)">function</span><span style="color: rgba(0, 0, 0, 1)"> IDEIndex() {
const allProjectTableRef </span>=<span style="color: rgba(0, 0, 0, 1)"> useRef();
</span><span style="color: rgba(0, 0, 255, 1)">return</span><span style="color: rgba(0, 0, 0, 1)"> (
</span><>
<AllProjectTable ref={allProjectTableRef}/>
<button onClick={() => allProjectTableRef.current.handleGetProjectList()}>调用子组件方法</button>
</>
<span style="color: rgba(0, 0, 0, 1)">);
}
export </span><span style="color: rgba(0, 0, 255, 1)">default</span> IDEIndex;</pre>
</div>
<p> </p>
<p><span style="font-size: 18px">useImperativeHandle 使用</span></p>
<div class="cnblogs_code">
<pre>useImperativeHandle(ref, createHandle, );</pre>
</div>
<p> useImperativeHandle 可以在使用 ref 时自定义暴露给父组件的实例值。在大多数情况下,应当避免使用 ref 这样的命令式代码。</p>
<p> useImperativeHandle 和 forwardRef 配合一起使用(如上子组件)</p>
<p> 1.ref:定义current 对象的 ref;</p>
<p> 2.createHandle:一个函数,返回值时一个对象,即这个 ref 的 current;</p>
<p> 3.对象 :即依赖列表,当监听的依赖发生变化,useImperativeHandle 才会重新将子组件的示例属性输出到父组件</p>
<p> 4.ref 的 current 属性上,如果为空数组,则不会重新输出</p>
<p> 在使用 useImperativeHandle 之前,要清除 React 关于 ref 转发(透传)这个知识点,主要是使用 React.forwardRef 方法实现的,该方法返回一个组件,参数为函数(并不是函数组件),函数的第一个参数为父组件传递的 props,第二个给父组件传递的 ref ,其目的就是希望可以在封装组件时,外层组件可以通过ref直接控制内层组件或元素的行为。</p>
<p> 正常情况下 ref 是不能挂载到函数组件上的,因为函数组件没有实例。React 官方为我们提供了 useImperativeHandle 一个类似实例的东西,帮助我们通过 useImperativeHandle 的第二个参数,把返回的对象的内容挂载到 父组件的 ref.current 上。</p>
<p> forwardRef 会创建一个 React 组件,这个组件能够将其接收的 ref 属性转发到其 组件树下的另一个组件中。</p>
<div class="cnblogs_code">
<pre>import React, {useImperativeHandle, forwardRef, useRef, useEffect} from 'react'<span style="color: rgba(0, 0, 0, 1)">;
const Children </span>= forwardRef((props, ref) =><span style="color: rgba(0, 0, 0, 1)"> {
useImperativeHandle(ref, () </span>=><span style="color: rgba(0, 0, 0, 1)"> ({
hello() {
console.log(</span>'访问到了子组件的方法'<span style="color: rgba(0, 0, 0, 1)">);
}
}));
});
</span><span style="color: rgba(0, 0, 255, 1)">function</span><span style="color: rgba(0, 0, 0, 1)"> Parent() {
const childrenRef </span>=<span style="color: rgba(0, 0, 0, 1)"> useRef();
useEffect(() </span>=><span style="color: rgba(0, 0, 0, 1)"> {
childrenRef.current.hello();
}, []);
</span><span style="color: rgba(0, 0, 255, 1)">return</span><span style="color: rgba(0, 0, 0, 1)"> (
</span><>
<Children ref={childrenRef}/>
</>
<span style="color: rgba(0, 0, 0, 1)">);
}
export </span><span style="color: rgba(0, 0, 255, 1)">default</span> forwardRef(Parent);</pre>
</div>
<p> </p><br><br>
来源:https://www.cnblogs.com/zhangning187/p/reactparentchildren111.html
頁:
[1]