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. 轻松上云怎么操作?IoT_CLOUD之中移OneNET

    ​  最近来了很多新朋友,也经常被问:可以多讲些云平台的操作吗?当然可以!文末留言你想要了解的云平台,优先安排~ 接下来,本文将以Air780E+LuatOS作为示例,教你使用合宙IoT_CLOUD连 ...

  2. 【网关开发】Openresty使用cosocket API 发送http与tcp网络请求

    背景 为网关提供健康检查功能时需要对节点发送http或者tcp探活请求.Openresty 提供cosocket来处理非阻塞IO. 实现 跟工程结合在一起,这里简单拼接数据结构 local funct ...

  3. flask 中的request【转载】

    每个框架中都有处理请求的机制(request),但是每个框架的处理方式和机制是不同的,为了了解flask的request中都有什么东西,首先我们要写一个前后端的交互 基于HTML+Flask 写一段前 ...

  4. 从架构到成本,SQL Server 和 PostgreSQL 四大区别全方位解析!

    从架构到成本,SQL Server 和 PostgreSQL 四大区别全方位解析! 今天我想分享 SQL Server 和 PostgreSQL 之间的四大关键区别. 在比较 SQL Server 和 ...

  5. cornerstone中raft_server_resp_handlers源码解析

    1.概述 在rpc请求里,有了请求req就必然有回复resp.本文就来解析发送req的节点收到resp该怎么处理. 2.handle_peer_resp源码解析 void raft_server::h ...

  6. 秒懂Redis

    一.redis简介 Redis 是C语言开发的一个开源高性能键值对的内存数据库,可以用来做数据库.缓存.消息中间件等场景,是一种NoSQL(not-only sql,非关系型数据库)的数据库 二.Re ...

  7. ZAFU五月多校合训

    B. 进制 jbgg 今天在幼儿园学了进制转换,现在 jbgg 有一个十进制正整数 \(x\),jbgg 好奇是否存在这样一个进制 \(p\),使得 \(x\) 在 \(p\) 进制表示下的各个位上的 ...

  8. ZCMU-1144

    简单问题: 就只是如何降低时间的问题罢了:本来这种方法以前学过但是没怎么用所以不太灵活. #include<stdio.h> #define maxn 1000010 int sum[ma ...

  9. HTML5 表单新的 Input 类型

    H5新增了电子邮箱,手机号码,网址,数量,搜索,范围,颜色选择,时间日期等input类型 1.电子邮箱 type="email" 提供电子邮箱格式验证 如果格式不对,会阻止表单提交 ...

  10. DA14531芯片固件逆向系列(1)-固件加载和逆向分析

    首发于先知论坛 https://xz.aliyun.com/t/9185 前言 本文介绍逆向DA14531芯片的固件,并介绍一些辅助自动化分析的脚本的实现.DA14531是Dialog公司研制的蓝牙芯 ...