当前位置: 首页> 科技> 能源 > 搜索引擎调词平台_文创产品网站_百度一下电脑版首页_网页设计模板html代码

搜索引擎调词平台_文创产品网站_百度一下电脑版首页_网页设计模板html代码

时间:2025/7/11 8:08:33来源:https://blog.csdn.net/lwl12345678_/article/details/143510154 浏览次数:0次
搜索引擎调词平台_文创产品网站_百度一下电脑版首页_网页设计模板html代码

父组件不管是类还是函数组件都支持creatRef(),函数组件可以用useRef();
子组件中,函数组件由于没有实例,只能使用forwardRef搭配useImperativeHandle使用

import React, { createRef,forwardRef} from 'react'
import styles from './index.module.scss'
import { useImperativeHandle } from 'react';
import { useRef } from 'react';class Test extends React.PureComponent {test() {console.log("Test111");}render() {return <h2>Test</h2>;}
}
// 创建一个函数组件, 作为子组件测试
// 使用forwardRef将函数包裹起来
const Foo = forwardRef(function(props, ref) {const testfoo = () => {console.log('testfoo');}useImperativeHandle(ref, () => {return {testfoo}},[])return (<h2 ref={ref}>Foo</h2>)
})// export default class About extends React.Component{
//   constructor(){
//     super()
//     this.testRef = createRef();
//     this.fooRef = createRef();
//   }
//   getDom() {
//     this.testRef.current.test();
//     console.log(this.fooRef.current,'current');
//     this.fooRef.current.testfoo();
//   }
//   render() {
//     return (
//       <div>
//         <Test ref={this.testRef}/>
//         <Foo  ref={this.fooRef}/>
//         <button onClick={() => this.getDom()}>获取DOM</button>
//       </div>
//     )
//   }
// }const About = () => {// const testRef = useRef();// const fooRef = useRef();const testRef = createRef();const fooRef = createRef();const getDom = () => {testRef.current.test();fooRef.current.testfoo();}return (<div><Test ref={testRef}/><Foo  ref={fooRef}/><button onClick={getDom}>获取DOM</button></div>)
}
export default About;
关键字:搜索引擎调词平台_文创产品网站_百度一下电脑版首页_网页设计模板html代码

版权声明:

本网仅为发布的内容提供存储空间,不对发表、转载的内容提供任何形式的保证。凡本网注明“来源:XXX网络”的作品,均转载自其它媒体,著作权归作者所有,商业转载请联系作者获得授权,非商业转载请注明出处。

我们尊重并感谢每一位作者,均已注明文章来源和作者。如因作品内容、版权或其它问题,请及时与我们联系,联系邮箱:809451989@qq.com,投稿邮箱:809451989@qq.com

责任编辑: