1、在dom元素中直接使用ref

意思就是可以在组件中创建一个dom节点的textInput,并将ref直接绑定到他

<script src="https://unpkg.com/@babel/standalone/babel.js"></script>
<script src="https://unpkg.com/react/umd/react.development.js"></script>
<script src="https://unpkg.com/react-dom/umd/react-dom.development.js"></script>
<div id="root"/>
<script type="text/babel">
//在dom元素上使用ref
class App extends React.Component{
constructor(props){
super(props)
this.textInput = React.createRef() (ref对象)
this.focusTextInput = this.focusTextInput.bind(this)
}
focusTextInput(){
this.textInput.current.focus()
}
render(){
return (
<div>
<input type="button" onClick={this.focusTextInput} value="获取焦点" />
<input type="text" ref={this.textInput} />
</div>
)
}
}
ReactDOM.render(<App />, document.getElementById('root'));
</script>

2、为class组件添加refs 这个时候 refs指向的是子组件的实例 这样在父组件中可以调用子组件中的方法

当为了使用更方便时可以直接在子组件上定义一个refs别名 作为props传递个子组件 

在父组件中想获取子组件的哪个dom元素 就将刚刚传递个子组件的props 作为dom元素ref属性的值

<script src="https://unpkg.com/@babel/standalone/babel.js"></script>
<script src="https://unpkg.com/react/umd/react.development.js"></script>
<script src="https://unpkg.com/react-dom/umd/react-dom.development.js"></script>
<div id="root"/>
<script type="text/babel">
class CustomeInput extends React.Component{
constructor(props){
super(props)
this.textInput = React.createRef()
this.textInputFocus = this.textInputFocus.bind(this) }
textInputFocus(){
this.textInput.current.focus()
}
render(){
return (
<React.Fragment>
<input readOnly type="button" value="点击我" />
<input onChange={()=>{}} ref={this.textInput} value="Focus the text input" />
</React.Fragment>
)
}
} class App extends React.Component{
constructor(props){
super(props)
this.textInput = React.createRef()
}
componentDidMount(){
this.textInput.current.textInputFocus()
}
render(){
return (
<div><CustomeInput ref={this.textInput} /></div>
)
}
} ReactDOM.render(<App />, document.getElementById('root'));
</script>
import React, { Component } from 'react';
import ReactDOM from 'react-dom'
import { ThemeContext,themes } from './components/ThemeContext';
import ThemeButton from './components/ThemeButton' class CustomInput extends React.Component{
constructor(props){
super(props)
}
render(){
return <input onChange={()=>{}} value="fouse" ref={this.props.inputRef} />
}
} class App extends React.Component{
constructor(props){
super(props)
this.input = React.createRef()
}
componentDidMount() {
this.input.current.focus()
}
render(){
return (
<div>
<CustomInput inputRef={this.input} />
</div>
)
}
} ReactDOM.render(<App />,document.getElementById('root'))

3、通过转发的方式将将 DOM Refs 暴露给父组件

<script src="https://unpkg.com/@babel/standalone/babel.js"></script>
<script src="https://unpkg.com/react/umd/react.development.js"></script>
<script src="https://unpkg.com/react-dom/umd/react-dom.development.js"></script>
<div id="root"/>
<script type="text/babel">
//利用forwardRef 将ref转发给dom元素的button
const FancyButton = React.forwardRef((props,ref)=>(
<button ref={ref} onClick={props.onClick}>
{props.children}
</button>
)) class App extends React.Component{
constructor(props){
super(props)
this.ref = React.createRef()
this.clickHandle = this.clickHandle.bind(this)
}
clickHandle(){
console.log(this.ref.current)
}
render(){
return (
<FancyButton ref={this.ref} onClick={this.clickHandle}>clickME</FancyButton>
)
}
}
</script>

 4、回调refs

注:内联的回调refs 会调用二次 第一次返回null

内联的写法:

<input ref={(e)=>this.inputText=el} onChange={()=>{}} value="inputfocus" />
<script src="https://unpkg.com/@babel/standalone/babel.js"></script>
<script src="https://unpkg.com/react/umd/react.development.js"></script>
<script src="https://unpkg.com/react-dom/umd/react-dom.development.js"></script>
<div id="root"/>
<script type="text/babel">
//回调refs
class App extends React.Component{
constructor(props){
super(props)
this.inputText=null;
this.setInputRef = (element)=>{
this.inputText = element;
}
this.focusTextInput=()=>{
if(this.inputText) this.inputText.focus()
}
}
componentDidMount(){
this.inputText.focus()
}
render(){
return (
<div>
<input type="button" value="点击" onClick={this.focusTextInput} />
<input ref={this.setInputRef} onChange={()=>{}} value="inputfocus" />
</div>
)
}
} ReactDOM.render(<App />, document.getElementById('root'));
</script>

react中refs的使用的更多相关文章

  1. React 中 refs 的作用是什么?

    Refs 是 React 提供给我们的安全访问 DOM 元素或者某个组件实例的句柄.我们可以为元素添加 ref 属性然后在回调函数中接受该元素在 DOM 树中的句柄,该值会作为回调函数的第一个参数返回 ...

  2. react中refs详解

    https://zh-hans.reactjs.org/docs/refs-and-the-dom.html 字符串形式ref 1 <input ref="myinput" ...

  3. React中refs持久化

    根据使用React的版本,选择合适的方法. 字符串模式 :废弃不建议使用 回调函数,React版本 < 16.3 React.createRef() :React版本 >= 16.3 回调 ...

  4. 九、React中的组件、父子组件、React props父组件给子组件传值、子组件给父组件传值、父组件中通过refs获取子组件属性和方法

    一.概述 React中的组件: 解决html 标签构建应用的不足. 使用组件的好处:把公共的功能单独抽离成一个文件作为一个组件,哪里里使用哪里引入. [父子组件]:组件的相互调用中,我们把调用者称为父 ...

  5. react中input自动聚焦问题

    input自动聚焦问题 在react中可以使用refs解决这个问题,首先看一下refs的使用场景: (1)处理焦点.文本选择或媒体控制. (2)触发强制动画. (3)集成第三方 DOM 库. 使用re ...

  6. react中这些细节你注意过没有?

    react中的一些细节知识点: 1.组件中get的使用(作为类的getter) ES6知识:class类也有自己的getter和setter,写法如下: Class Component { const ...

  7. React中ref的使用方法

    React中ref的使用方法 在react典型的数据流中,props传递是父子组件交互的唯一方式:通过传递一个新的props值来使子组件重新re-render,从而达到父子组件通信.当然,就像reac ...

  8. React中ref的用法

    在React数据流中,父子组件唯一的通信方式是通过props属性:那么如果有些场景需要获取某一个真实的DOM元素来交互,这时候就要用到React的refs属性. 1.可以给DOM元素添加ref属性 c ...

  9. React中setState学习总结

    react中setState方法到底是异步还是同步,其实这个是分在什么条件下是异步或者同步. 1.先来回顾一下react组件中改变state的几种方式: import React, { Compone ...

  10. react中父组件给子组件传值

    子组件 state = { msg: 'a' } render(){ return<h1>{this.state.msg}</h1> } setInfo = (val)=> ...

随机推荐

  1. 基于sqli-labs Less-1的sql联合注入详解

    SQLi Labs 是一个专为学习和测试 SQL 注入漏洞设计的实验平台,旨在帮助安全研究人员.开发者和网络安全爱好者深入理解并实践各种 SQL 注入攻击.该平台提供了一系列精心设计的实验环境,模拟真 ...

  2. 命运的X

    命运的X cjx 生成函数强. 思路 首先,设 \(f_i\) 为添加第 \(i\) 项后满足条件的概率,\(g_i\) 任意添加至第 \(i\) 项的概率. 我们要求的答案: \[ans=\sum_ ...

  3. 销讯通CRM系统如何管理医药代表的销售过程

    医药行业的销售代表与其他行业的销售代表在专业知识要求.客户群体.销售流程.以及行业特性等方面都存在明显的区别,他们必须具备更高的专业素养和综合能力. CRM(客户关系管理系统)在医药行业中对于管理医药 ...

  4. 自有Jar包生成Docker镜像

    前言 经常会有些自己写的一些SpringBoot小项目,为了实现一些小的功能/需求,但是部署的时候,不管是生成jar包,还是war包部署到tomcat中,都容易因为需要部署的环境(比如java版本.t ...

  5. chrome浏览器设置允许跨域

    前情 在访问测试搭建的测试环境的时候,发现接口因为跨域全部失败了,服务端又不想设置允许跨域,又急于使用,于是想到是不是可以使用跨域浏览器 放开chrome的跨域设置步骤 复制一个chrome快捷图标, ...

  6. GienTech动态|入选软件和信息技术服务竞争力百强;参展世界计算大会、网络安全博览会

    ​ ​ ---- GienTech动态 ---- 中电金信参展广东省网络安全博览会.世界计算机大会 ​ ​ 近期,中电金信跟随中国电子参展2023年广东省网络安全博览会(下简称"博览会&qu ...

  7. TypeScript 源码详细解读(2)词法1-字符处理

    本节文章研究的代码位于 tsc/src/compiler/scanner.ts 字符 任何源码都是由很多字符组成的,这些字符可以是字母.数字.空格.符号.汉字等-- 每一个字符都有一个编码值,比如字符 ...

  8. 2024年1月Java项目开发指南9:密码加密存储

    提前声明: 你不会写这加密算法没关系啊,你会用就行. 要求就是:你可以不会写这个加密算法,但是你要知道加密流程,你要会用. @Service public class PasswordEncrypto ...

  9. 性能测试 -- docker部署grafana

    一.前提 1.安装好了docker 2.docker内 和 jmeter脚本 全都已经部署好了influxdb,并且数据采集等都正常 二.docker 部署 grafana 的操作步骤 1.下载 do ...

  10. ubuntu安装ps命令

    docker容器是debian的镜像,没有ps命令,查个进程没法查. 安装procps包 apt-get install procps