react中refs的使用
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的使用的更多相关文章
- React 中 refs 的作用是什么?
Refs 是 React 提供给我们的安全访问 DOM 元素或者某个组件实例的句柄.我们可以为元素添加 ref 属性然后在回调函数中接受该元素在 DOM 树中的句柄,该值会作为回调函数的第一个参数返回 ...
- react中refs详解
https://zh-hans.reactjs.org/docs/refs-and-the-dom.html 字符串形式ref 1 <input ref="myinput" ...
- React中refs持久化
根据使用React的版本,选择合适的方法. 字符串模式 :废弃不建议使用 回调函数,React版本 < 16.3 React.createRef() :React版本 >= 16.3 回调 ...
- 九、React中的组件、父子组件、React props父组件给子组件传值、子组件给父组件传值、父组件中通过refs获取子组件属性和方法
一.概述 React中的组件: 解决html 标签构建应用的不足. 使用组件的好处:把公共的功能单独抽离成一个文件作为一个组件,哪里里使用哪里引入. [父子组件]:组件的相互调用中,我们把调用者称为父 ...
- react中input自动聚焦问题
input自动聚焦问题 在react中可以使用refs解决这个问题,首先看一下refs的使用场景: (1)处理焦点.文本选择或媒体控制. (2)触发强制动画. (3)集成第三方 DOM 库. 使用re ...
- react中这些细节你注意过没有?
react中的一些细节知识点: 1.组件中get的使用(作为类的getter) ES6知识:class类也有自己的getter和setter,写法如下: Class Component { const ...
- React中ref的使用方法
React中ref的使用方法 在react典型的数据流中,props传递是父子组件交互的唯一方式:通过传递一个新的props值来使子组件重新re-render,从而达到父子组件通信.当然,就像reac ...
- React中ref的用法
在React数据流中,父子组件唯一的通信方式是通过props属性:那么如果有些场景需要获取某一个真实的DOM元素来交互,这时候就要用到React的refs属性. 1.可以给DOM元素添加ref属性 c ...
- React中setState学习总结
react中setState方法到底是异步还是同步,其实这个是分在什么条件下是异步或者同步. 1.先来回顾一下react组件中改变state的几种方式: import React, { Compone ...
- react中父组件给子组件传值
子组件 state = { msg: 'a' } render(){ return<h1>{this.state.msg}</h1> } setInfo = (val)=> ...
随机推荐
- 多校A层冲刺NOIP2024模拟赛20
多校A层冲刺NOIP2024模拟赛20 昨天晚上打 ABC 了,所以今天才发. T1 星际联邦 直接上 菠萝(Borůvka)算法就行了,当然还可以用线段树优化 prim算法,但是没打过只是口胡:就是 ...
- KindleVocab 教程,Kindle导出查词记录成文本文档,Kindle导出查询单词记录导入Anki
程序功能 因本人使用Kindle Mate导出觉得复杂,特意写了个自用的导出程序(有linux版本和win两个版本). 所以 KindleVocab 只有一个作用:导出Kindle查询过的生词和生词所 ...
- 使用WebRTC技术搭建小型的视频聊天页面
目录 目录 参考资料 什么是WebRTC? 能做什么? 架构图 个人理解(类比) 核心知识点 核心知识点类比 ICE框架 STUN(协议) NAT(网络地址转换) TURN SDP(会话描述协议) W ...
- php open_basedir的使用
今天跨省问为什么file_exists检测一个相对路径的文件无法获取到true,文件明明有,但是获取不到,我看了一下,感觉可能是因为这个文件是软链接过来的有关系. 然后他找了找发现是和这么一个文件.u ...
- javaScript 的面向对象程序
理解对象 属性类型 数据属性(数据属性包含一个数据值的位置,这个位置可以读取和写入值,数据属性有4描述) [Configurable]:表示能否通过 delete 删除属性从而重新定义属性,能否修改属 ...
- dotnet学习笔记-专题06-过滤器和中间件-01
1. 基本概念 在ASP.NET Core中,中间件和过滤器都是处理HTTP请求的重要组件,但它们在应用中的位置.作用范围以及使用方式有所不同. 1.1 中间件和过滤器的区别 1.1.1 中间件 位置 ...
- json-lib(ezmorph)、gson、flexJson、fastjson、jackson对比,实现java转json,json转java
json-lib(ezmorph).gson.flexJson.fastjson.jackson对比,实现java转json,json转java 本文中所讲的所有代码都在此:json-test 目前关 ...
- JPEG格式研究——(4)反量化、逆ZigZag变化和IDCT变换
反量化 反量化其实很简单,将霍夫曼解码出来的数据乘上对应的量化表就好了 通过当前色度选择出SOF中的Component,其中的Tqi指出了这一色度所需的量化表id Component的结构如下: 名称 ...
- Mybatis【18】-- Mybatis自关联多对一查询方式
注:代码已托管在GitHub上,地址是:https://github.com/Damaer/Mybatis-Learning ,项目是mybatis-14-oneself-many2one,需要自取, ...
- wait、notify、notifyAll
介绍 wait 方法前提需要拥有锁.使用wait方法后,释放锁进行等待队列. notify 方法从等待队列移除一个元素. notifyAll 将等待队列中元素全部进行移出. 注意:notify.not ...