[React] Use React ref to Get a Reference to Specific Components
When you are using React components you need to be able to access specific references to individual component instances. This is done by defining a ref. This lesson will introduce us to some of the nuances when using ref.
<input
ref="b"
type="text"
onChange={this.update.bind(this)}
/>
The way to refer to the 'ref':
this.refs.b.value
Also 'ref' is able to receive a callback function:
<Input
ref={ component => this.a = component}
update={this.update.bind(this)}
/> class Input extends React.Component {
render(){
return <div><input ref="input" type="text" onChange={this.props.update}/></div>
}
}
Now the way to access the ref value:
this.a.refs.input.value,
class App extends React.Component {
constructor(){
super();
this.state = {a: '', b: ''}
}
update(){
this.setState({
a: this.a.refs.input.value,
b: this.refs.b.value
})
}
render(){
return (
<div>
<Input
ref={ component => this.a = component}
update={this.update.bind(this)}
/> {this.state.a}
<hr />
<input
ref="b"
type="text"
onChange={this.update.bind(this)}
/> {this.state.b}
</div>
)
}
}
class Input extends React.Component {
render(){
return <div><input ref="input" type="text" onChange={this.props.update}/></div>
}
}
ReactDOM.render(
<App />,
document.getElementById('root')
);
[React] Use React ref to Get a Reference to Specific Components的更多相关文章
- React Native中ref的用法(通过组件的ref属性,来获取真实的组件)
ref是什么? ref是组件的特殊属性,组件被渲染后,指向组件的一个引用.可以通过组件的ref属性,来获取真实的组件.因为,组件并不是真正的DOM节点,而是存在于内存中的一种数据结构,称为虚拟的DOM ...
- 六、React 键盘事件 表单事件 事件对象以及React中的ref获取dom节点 、React实现类似Vue的双向数据绑定
接:https://www.cnblogs.com/chenxi188/p/11782349.html 事件对象 .键盘事件. 表单事件 .ref获取dom节点.React实现类似vue双向数据绑定 ...
- React组件proptypes, ref
一.使用props.children访问嵌套数据 import React from 'react'; class Button extends React.Component { render () ...
- react中的ref的3种方式
2020-03-31 react中的ref的3种方式 react中ref的3种绑定方式 方式1: string类型绑定 类似于vue中的ref绑定方式,可以通过this.refs.绑定的ref的名字获 ...
- JavaScript 和 React,React用了大量语法糖,让JS编写更方便。
https://reactjs.org/docs/higher-order-components.htmlhttps://codepen.io/gaearon/pen/WooRWa?editors=0 ...
- React的React.createRef()/forwardRef()源码解析(三)
1.refs三种使用用法 1.字符串 1.1 dom节点上使用 获取真实的dom节点 //使用步骤: 1. <input ref="stringRef" /> 2. t ...
- React Hooks & react forwardRef hooks & useReducer
React Hooks & react forwardref hooks & useReducer react how to call child component method i ...
- react之react Hooks
函数组件,没有 class 组件中的 componentDidMount.componentDidUpdate 等生命周期方法,也没有 State,但这些可以通过 React Hook 实现. Rea ...
- React学习笔记-1-什么是react,react环境搭建以及第一个react实例
什么是react?react的官方网站:https://facebook.github.io/react/下图这个就是就是react的标志,非常巧合的是他和我们的github的编辑器Atom非常相似. ...
随机推荐
- 洛谷 P1755 斐波那契的拆分
P1755 斐波那契的拆分 题目背景 无 题目描述 已知任意一个正整数都可以拆分为若干个斐波纳契数,现在,让你求出n的拆分方法 输入输出格式 输入格式: 一个数t,表示有t组数据 接下来t行,每行一个 ...
- matlab 文件路径问题
1. fullfile:路径补全 f = fullfile(filepart1,...,filepartN) 显然可变参数之间填充的路径分隔符(path separator),会根据操作系统而变化: ...
- Redis的高级应用-安全性和主从复制
Redis的服务器命令和键值命令(String,Hash,List,Set,Zset)相对简单,只需查看文档即可. 文档地址: http://www.runoob.com/redis/redis-tu ...
- 【习题 6-1 UVA-673】Parentheses Balance
[链接] 我是链接,点我呀:) [题意] 在这里输入题意 [题解] 括号匹配. 栈模拟就好. 多种括号也是一样可以做的. [代码] #include <bits/stdc++.h> usi ...
- HTTP网络协议(三)
HTTP首部字段有四种类型:通用首部字段,请求首部字段,响应首部字段,实体首部字段. 通用首部字段: 首部字段 说明 Cache-Control 控制缓存的行为 Connection 逐跳首部.连接 ...
- HTTP详解--请求、响应、缓存
1. HTTP请求格式 做过Socket编程的人都知道,当我们设计一个通信协议时,“消息头/消息体”的分割方式是很常用的,消息头告诉对方这个消息是干什么的,消息体告诉对方怎么干.HTTP协议传输的消息 ...
- Windows下Nginx的下载安装、启动停止和配置浏览
前言: 记录一下今天在Windows下载安装Nginx服务器的过程.因为网上关于Nginx的资料都太复杂了,大多数是在Linux下使用的方法. 1.下载 Nginx官网下载地址:http://ngin ...
- SQL Server 2008 Tempdb 数据库迁移
1.首先检查数据文件位置及名称 SELECT name,physical_name FROM sys.database_files 2.迁移 USE master; GO ALTER DATABASE ...
- 中间件、服务器和Web服务器三者的区别
相信很多的Web安全初学者和我一样,对中间件和服务器的认识不够深刻,对两者的概念可能会有所混淆. 正好今天在学习的时候突然想到了这个问题,粗略百度了一下,似乎网上对这个问题的解释不多,那么就由我来为大 ...
- 【例题5-8 UVA - 400】Unix ls
[链接] 我是链接,点我呀:) [题意] 在这里输入题意 [题解] 设n个字符串中出现的最长的为len; 最后一列能容纳len个字符,然后前面的列能容纳len+2个字符. 每行最多60个字符. 按照这 ...