React-Ref

[复制链接]
发表于 2026-1-23 18:10:14 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能,让你轻松玩转社区。

您需要 登录 才可以下载或查看,没有账号?立即注册

×
1. React中获取元素的方式

原生DOM(不保举)
通过ref获取(保举)
字符串
对象
回调函数
原生DOM获取元素(不保举)
非常非常不保举,由于这种情况是通过拿到真实DOM,而react创建元素大多数时间是通过假造DOM创建的
  1. import React from 'react';
  2. class App extends React.PureComponent{
  3.    
  4.     constructor(props){
  5.    
  6.         super(props);
  7.     }
  8.     render(){
  9.    
  10.         console.log('App-render被调用');
  11.         return (
  12.             <div>
  13.                 <p id={
  14.    'box'}>我是段落</p>
  15.                 <button onClick={
  16.    ()=>{
  17.    this.btnClick()}}>APP按钮</button>
  18.             </div>
  19.         )
  20.     }
  21.     btnClick(){
  22.    
  23.         // 第一种获取方式: 传统方式(在React中及其不推荐)
  24.         let oP = document.getElementById('box');
  25.         oP.innerHTML = 'www.it666.com';
  26.         console.log(oP);
  27.     }
  28. }
  29. export default App;
复制代码
第二种方式:通过refs团结字符串举行获取(react即将废弃)

通过ref='字符串' / this.refs.字符串 (通过字符串的方式即将被废弃, 也不保举)
  1. import React from 'react';
  2. class App extends React.PureComponent{
  3.    
  4.     constructor(props){
  5.    
  6.         super(props);
  7.     }
  8.     render(){
  9.    
  10.         console.log('App-render被调用');
  11.         return (
  12.             <div>
  13.                 // 1. 给获取的元素添加refs
  14.                 <p ref={
  15.    'box'}>我是段落</p>
  16.                 <button onClick={
  17.    ()=>{
  18.    this.btnClick()}}>APP按钮</button>
  19.             </div>
  20.         )
  21.     }
  22.     btnClick(){
  23.    
  24.         // 第二种获取方式: 通过ref='字符串' / this.refs.字符串 (通过字符串的方式即将被废弃, 也不推荐)
  25.         let oP = this.refs.box;
  26.         oP.innerHTML = 'www.it666.com';
  27.         console.log(oP);
  28.     }
  29. }
  30. export default App;
复制代码
第三种获取方式:通过refs+对象获取

通过createRef()创建一个对象, 然后将这个对象通报给ref (保举)
  1. import React from 'react';
  2. class App extends React.PureComponent{
  3.    
  4.     constructor(props){
  5.    
  6.         super(props);
  7. // 1.先创建一个Ref对象
  8.         this.oPRef = React.createRef();
  9.         this.oPRef = null;
  10.     }
  11.     render(){
  12.    
  13.         console.log('App-render被调用');
  14.         return (
  15.             <div>
  16.                 <p ref={
  17.    this.oPRef}>我是段落</p>
  18.                 <button onClick={
  19.    ()=>{
  20.    this.btnClick()}}>APP按钮</button>
  21.             </div>
  22.         )
  23.     }
  24.     btnClick(){
  25.    
  26.        // 2. 第三种获取方式: 通过createRef()创建一个对象, 然后将这个对象传递给ref (推荐)
  27.         let oP = this.oPRef.current;
  28.         oP.innerHTML = 'www.it666.com';
  29.         console.log(oP);
  30.     }
  31. }
  32. export default App;
复制代码
第四种方式:通过函数返回组件内容

第四种获取方式: 通过通报一个回调函数, 然后生存回调函数参数的方式(保举)
  1. import React from 'react';
  2. class App extends React.PureComponent{
  3.    
  4.     constructor(props){
  5.    
  6.         super(props);
  7.         // 定义一个变量对象为空
  8.         this.oPRef = null;
  9.     }
  10.     render(){
  11.    
  12.         console.log('App-render被调用');
  13.         return (
  14.             <div>
  15.               //参数就是该元素,将arg赋值给OPRef
  16.                 <p ref={
  17.    (arg)=>{
  18.    this.oPRef = arg}}>我是段落</p>
  19.                 <button onClick={
  20.    ()=>{
  21.    this.btnClick()}}>APP按钮</button>
  22.             </div>
  23.         )
  24.     }
  25.     btnClick(){
  26.    
  27.         // 第四种获取方式: 通过传递一个回调函数, 然后保存回调函数参数的方式(推荐)
  28.         let oP = this.oPRef;
  29.         oP.innerHTML = 'www.it666.com';
  30.         console.log(oP);
  31.     }
  32. }
  33. export default App;
复制代码
根据欣赏器的报错提示Do you mean to use React.forwardRef()?
其着实react中固然拿不到函数式组件的ref,但是可以大概通过React.forwardRef()拿到组件内部的内容,如下
  1. // props接收父组件的数据;myRef接收外部传进来的ref
  2. const About = React.forwardRef(function(props, myRef) {
  3.     // myRef === this.myRef
  4.     return (
  5.         <div>
  6.             <p>我是段落</p>
  7.             <span ref={
  8.    myRef}>我是span</span>
  9.         </div>
  10.     )
  11. });
  12. class App extends React.PureComponent{
  13.    
  14.     constructor(props){
  15.    
  16.         super(props);
  17.         this.myRef = React.createRef();
  18.     }
  19.     render(){
  20.    
  21.         return (
  22.             <div>
  23. // 在自定义组件外部赋予属性ref={this.myRef}
  24.                 <About ref={
  25.    this.myRef}/>
  26.                 <button onClick={
  27.    ()=>{
  28.    this.btnClick()}}>APP按钮</button>
  29.             </div>
  30.         )
  31.     }
  32.     btnClick(){
  33.    
  34.         console.log(this.myRef.current);
  35. //   <span>我是span</span>
  36.     }
  37. }
  38. export default App;
复制代码
这种征象叫做Ref转发
回复

使用道具 举报

登录后关闭弹窗

登录参与点评抽奖  加入IT实名职场社区
去登录
快速回复 返回顶部 返回列表