合规国际互联网加速 OSASE为企业客户提供高速稳定SD-WAN国际加速解决方案。 广告
![](https://img.kancloud.cn/79/94/7994f91d53ce16f4852d7a8cb67c63d3_1724x1028.png) **例子** ``` import React from "react"; export default class RefDemo extends React.Component{ constructor(props) { super(props); this.objRef = React.createRef(); } componentDidMount() { setTimeout(()=>{ this.refs.stringRef.textContent = 'string ref got'; this.methodRef.textContent = 'methodRef ref got'; this.objRef.current.textContent = 'objRef ref got'; }) } render() { return( <> <p ref = "stringRef"> span1 </p> <p ref = {ele =>(this.methodRef = ele)}> span3 </p> <p ref = { this.objRef }> span3 </p> </> ) } } ``` 源码 文件地址: ``` packages/react/src/ReactCreateRef.js ``` ~~~ import type {RefObject} from 'shared/ReactTypes'; // an immutable object with a single mutable value export function createRef(): RefObject { const refObject = { current: null, }; return refObject; } ~~~