refs
Refs 提供了一种方式,允许我们访问 DOM 节点或在 render 方法中创建的 React 元素
创建refs
字符串形式refs(过时了)
| |
| class Demo extends React.Component { |
| showData = () => { |
| |
| const { input1 } = this.refs; |
| alert(input1.value); |
| } |
| showData2 = () => { |
| const { input2 } = this.refs; |
| alert(input2.value); |
| } |
| render() { |
| return ( |
| <div> |
| <input ref="input1" type="text" placeholder="点击按钮提示数据" /> |
| <button onClick={this.showData}>点我提示</button> |
| <input onBlur={this.showData2} ref="input2" type="text" placeholder="失去焦点提示" /> |
| </div> |
| ) |
| } |
| } |
回调函数形式refs
| |
| class Demo extends React.Component { |
| showData = () => { |
| |
| const { input1 } = this; |
| alert(input1.value); |
| } |
| showData2 = () => { |
| const { input2 } = this; |
| alert(input2.value); |
| } |
| |
| showInfo = (c) => { |
| this.input2 = c; |
| } |
| render() { |
| return ( |
| <div> |
| <input ref={c => this.input1 = c} type="text" placeholder="点击按钮提示数据" /> |
| <button onClick={this.showData}>点我提示</button> |
| <input ref={this.showInfo} onBlur={this.showData2} type="text" placeholder="失去焦点提示" /> |
| </div> |
| ) |
| } |
| } |
createRef(react16.3后)
| class Demo extends React.Component { |
| |
| |
| |
| myRef = React.createRef(); |
| showData = () => { |
| |
| const value = this.myRef.current.value; |
| alert(value); |
| } |
| render() { |
| return ( |
| <div> |
| <input ref={this.myRef} type="text" placeholder="点击按钮提示数据" /> |
| <button onClick={this.showData}>点我提示</button> |
| </div> |
| ) |
| } |
| } |
访问refs
当 ref 被传递给 render
中的元素时,对该节点的引用可以在 ref 的 current
属性中被访问。
| const node = this.myRef.current; |
ref 的值根据节点的类型而有所不同:
- 当
ref
属性用于 HTML 元素时,构造函数中使用 React.createRef()
创建的 ref
接收底层 DOM 元素作为其 current
属性。
- 当
ref
属性用于自定义 class 组件时,ref
对象接收组件的挂载实例作为其 current
属性。
- 不能在函数组件上使用
ref
属性,因为他们没有实例
为Dom元素添加ref
| class CustomTextInput extends React.Component { |
| constructor(props) { |
| super(props); |
| |
| this.textInput = React.createRef(); |
| this.focusTextInput = this.focusTextInput.bind(this); |
| } |
| |
| focusTextInput() { |
| |
| |
| this.textInput.current.focus(); } |
| |
| render() { |
| |
| |
| return ( |
| <div> |
| <input |
| type="text" |
| ref={this.textInput} /> |
| <input |
| type="button" |
| value="Focus the text input" |
| onClick={this.focusTextInput} |
| /> |
| </div> |
| ); |
| } |
| } |
React 会在组件挂载时给 current
属性传入 DOM 元素,并在组件卸载时传入 null
值。ref
会在 componentDidMount
或 componentDidUpdate
生命周期钩子触发前更新。
为 class 组件添加 Ref
| class AutoFocusTextInput extends React.Component { |
| constructor(props) { |
| super(props); |
| this.textInput = React.createRef(); } |
| |
| componentDidMount() { |
| this.textInput.current.focusTextInput(); } |
| |
| render() { |
| return ( |
| <CustomTextInput ref={this.textInput} /> ); |
| } |
| } |
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 阿里最新开源QwQ-32B,效果媲美deepseek-r1满血版,部署成本又又又降低了!
· 开源Multi-agent AI智能体框架aevatar.ai,欢迎大家贡献代码
· Manus重磅发布:全球首款通用AI代理技术深度解析与实战指南
· 被坑几百块钱后,我竟然真的恢复了删除的微信聊天记录!
· AI技术革命,工作效率10个最佳AI工具