当前位置:首页 > React

react ref 如何使用

2026-02-12 06:55:36React

React Refs 的基本概念

Refs 提供了一种直接访问 DOM 节点或 React 组件实例的方式。通常用于需要直接操作 DOM 或触发组件方法的场景,例如输入框聚焦、动画控制等。

创建 Refs

使用 React.createRef()useRef Hook 创建 ref 对象。

类组件中使用 createRef

class MyComponent extends React.Component {
  constructor(props) {
    super(props);
    this.myRef = React.createRef();
  }

  render() {
    return <div ref={this.myRef} />;
  }
}

函数组件中使用 useRef

function MyComponent() {
  const myRef = React.useRef(null);

  return <div ref={myRef} />;
}

访问 Refs

通过 ref 对象的 current 属性访问 DOM 节点或组件实例。

访问 DOM 节点

function TextInput() {
  const inputRef = React.useRef(null);

  const focusInput = () => {
    inputRef.current.focus();
  };

  return (
    <div>
      <input ref={inputRef} type="text" />
      <button onClick={focusInput}>Focus Input</button>
    </div>
  );
}

访问类组件实例

class CustomInput extends React.Component {
  focus() {
    this.inputRef.current.focus();
  }

  render() {
    return <input ref={this.inputRef} />;
  }
}

function ParentComponent() {
  const customInputRef = React.useRef(null);

  const handleClick = () => {
    customInputRef.current.focus();
  };

  return (
    <div>
      <CustomInput ref={customInputRef} />
      <button onClick={handleClick}>Focus Custom Input</button>
    </div>
  );
}

回调 Refs

回调 ref 是一种动态设置 ref 的方式,适用于 ref 需要动态变化的情况。

function CallbackRefExample() {
  let textInput = null;

  const setTextInputRef = (element) => {
    textInput = element;
  };

  const focusTextInput = () => {
    if (textInput) textInput.focus();
  };

  return (
    <div>
      <input type="text" ref={setTextInputRef} />
      <button onClick={focusTextInput}>Focus Input</button>
    </div>
  );
}

转发 Refs(Forwarding Refs)

通过 React.forwardRef 将 ref 传递给子组件,适用于高阶组件或封装通用逻辑的组件。

react ref 如何使用

const FancyButton = React.forwardRef((props, ref) => (
  <button ref={ref} className="fancy-button">
    {props.children}
  </button>
));

function App() {
  const buttonRef = React.useRef(null);

  return (
    <div>
      <FancyButton ref={buttonRef}>Click Me</FancyButton>
    </div>
  );
}

注意事项

  • 避免过度使用 refs,优先使用 React 的状态和属性机制。
  • 函数组件没有实例,不能直接通过 ref 访问,除非使用 forwardRefuseImperativeHandle
  • 在组件挂载后(componentDidMountuseEffect)才能安全访问 ref。

分享给朋友:

相关文章

react 如何继承

react 如何继承

在React中,组件继承并非推荐的设计模式(官方更推崇组合优于继承),但技术上仍可通过以下方式实现类似效果: 使用ES6类继承 通过extends关键字继承父组件类,子组件可访问父组件的生命…

react中monent如何获取日期

react中monent如何获取日期

使用 Moment.js 获取当前日期 在 React 中通过 Moment.js 获取当前日期,可以直接调用 moment() 函数。它会返回包含当前日期和时间的 Moment 对象。 impor…

react 如何分页

react 如何分页

分页实现方法 在React中实现分页功能可以通过多种方式完成,具体取决于数据来源(如API或本地数据)和UI库的选择。以下是常见的实现方法: 使用本地数据分页 对于存储在组件状态或Context中…

react如何读

react如何读

React 的发音 React 的发音为 /riˈækt/(音标),读作“瑞-艾克特”。其中: “Re” 发音类似英文单词 “read” 的开头部分。 “act” 发音与英文单词 “act” 一致。…

react 如何运行

react 如何运行

运行 React 项目的步骤 安装 Node.js 确保系统已安装 Node.js(建议版本 16+),可通过官网下载并安装。安装后验证版本: node -v npm -v 创建 React 项目…

react项目如何

react项目如何

创建React项目 使用create-react-app工具快速初始化项目。确保已安装Node.js(建议版本≥12.0.0),运行以下命令生成新项目: npx create-react-app m…