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的更多相关文章

  1. React Native中ref的用法(通过组件的ref属性,来获取真实的组件)

    ref是什么? ref是组件的特殊属性,组件被渲染后,指向组件的一个引用.可以通过组件的ref属性,来获取真实的组件.因为,组件并不是真正的DOM节点,而是存在于内存中的一种数据结构,称为虚拟的DOM ...

  2. 六、React 键盘事件 表单事件 事件对象以及React中的ref获取dom节点 、React实现类似Vue的双向数据绑定

    接:https://www.cnblogs.com/chenxi188/p/11782349.html 事件对象 .键盘事件. 表单事件 .ref获取dom节点.React实现类似vue双向数据绑定 ...

  3. React组件proptypes, ref

    一.使用props.children访问嵌套数据 import React from 'react'; class Button extends React.Component { render () ...

  4. react中的ref的3种方式

    2020-03-31 react中的ref的3种方式 react中ref的3种绑定方式 方式1: string类型绑定 类似于vue中的ref绑定方式,可以通过this.refs.绑定的ref的名字获 ...

  5. JavaScript 和 React,React用了大量语法糖,让JS编写更方便。

    https://reactjs.org/docs/higher-order-components.htmlhttps://codepen.io/gaearon/pen/WooRWa?editors=0 ...

  6. React的React.createRef()/forwardRef()源码解析(三)

    1.refs三种使用用法 1.字符串 1.1 dom节点上使用 获取真实的dom节点 //使用步骤: 1. <input ref="stringRef" /> 2. t ...

  7. React Hooks & react forwardRef hooks & useReducer

    React Hooks & react forwardref hooks & useReducer react how to call child component method i ...

  8. react之react Hooks

    函数组件,没有 class 组件中的 componentDidMount.componentDidUpdate 等生命周期方法,也没有 State,但这些可以通过 React Hook 实现. Rea ...

  9. React学习笔记-1-什么是react,react环境搭建以及第一个react实例

    什么是react?react的官方网站:https://facebook.github.io/react/下图这个就是就是react的标志,非常巧合的是他和我们的github的编辑器Atom非常相似. ...

随机推荐

  1. Spring学习总结(9)——Spring AOP总结

    spring IOC和AOP是Spring框架的两大核心基石,本文将对Spring AOP做一个系统的总结. 什么是AOP AOP(Aspect-Oriented Programming,面向切面编程 ...

  2. 【SonicUI】关于字体高亮的问题。。

    m_pSonicString[1]->Format(_T("/c=%x, a='http://hi.csdn.net/', linkh=0xFF00F0, font, font_hei ...

  3. amazeui学习笔记一(开始使用3)--兼容性列表compatibility

    amazeui学习笔记一(开始使用3)--兼容性列表compatibility 一.总结 1.不要用ie做前端测试,不要碰ie,尽量用google 浏览器: 按照微软官方的说法,IE 开发者工具中的浏 ...

  4. 给已有数据的oracle表建立外键关系

    PS:这里是给自己做个备忘,下次遇到同类问题的时候,方便查找: 客户在有主外键关系的2张表进行页面删除时报错已有子记录,运维后台处理的时候应该找出相应的数据,先删除子记录,在删主表记录:但客户要的急, ...

  5. css页面滚动条出现时防止页面跳动的方法

    大家写页面时应该都遇到过一个问题,尤其是写单页面应用的时候, 在有滚动条页面和没有滚动条页面之间相互跳转时, 你页面的主体内容会向左或者向右抖一下,让强迫症看了很不舒服. 现在就来解救一下强迫症: 方 ...

  6. JS学习笔记 - 运动 - 淘宝轮播图

    <script> window.onload=function () { var oDiv=document.getElementById('play'); var aBtn=oDiv.g ...

  7. ZOJ 1586 QS Network MST prim水题

    http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemId=586 题目大意: QS是一种生物,要完成通信,需要设备,每个QS需要的设备的价格 ...

  8. 通过Rman catalog 创建及管理Oracle数据库备份

    基本环境信息target DB (需备份数据库) 192.168.199.67 ORACLE_SID=zgw HOSTNAME=Oracle11 catlog DB (备份管理数据库) 192.168 ...

  9. Centos NFS 简单设置

    Server 端: NFS的安装配置:centos 5 :yum install nfs-utils portmapcentos 6 :yum install nfs-utils rpcbind vi ...

  10. (转)Could not execute auto check for display colors using command /usr/bin/xdpyinfo. Check if the DISPL

    转自:http://blog.csdn.net/huashnag/article/details/9357517 Starting Oracle Universal Installer... Chec ...