ReactNative进阶---Refs
来源:互联网 发布:日语网络 编辑:程序博客网 时间:2024/06/05 17:32
ReactNative进阶—Refs
一、简介
有时我们需要组件的非典型数据或者方法,并不是以属性的方式出现,而是通过组件的引用调用。一般常见的使用场景有:管理焦点、文本选择、媒体播放、触发必要的动画、合并第三方库等等。下面让我们通过几个简单的demo来认识一下refs。
二、基础知识
- ref是组件的一个特殊属性,任何一个组件都带有该属性。
- ref属性通过callback获取组件引用。
- callback调用时机:在组件装备完成或者卸载完成之后执行。
- 获取引用的情况:1. 同类别中 2. 子组件暴露引用给父组件
三、应用
- 同类别中获取组件引用
import React, { Component } from 'react';import { TextInput, TouchableOpacity, View, Text } from 'react-native';export default class RefsBasic extends Component { _handleClick = () => { if(this.textInput) { // 使输入框获取光标 this.textInput.focus(); } }; render() { return( <View style={{ padding:20 }}> <TextInput ref={ input => this.textInput = input } style={{ height: 40 }} placeholder="请输入数据" /> <TouchableOpacity onPress={ this._handleClick }> <Text>Click</Text> </TouchableOpacity> </View> ); }}
- 子组件暴露引用给父组件,在父组件中操作子组件相关属性和方法
import React, { Component } from 'react';import { TextInput, TouchableOpacity, View, Text } from 'react-native';export default class RefsBasic extends Component { // 处理点击事件 _handleClickCustom = () => { if(this.customTextInput) { // 使输入框获取光标 this.customTextInput.focus(); } }; render() { return( <View style={{ padding:20 }}> // 引用子组件 <CustomTextInput // 获取子组件的引用 inputRef={ ref => this.customTextInput = ref } /> <TouchableOpacity onPress={ this._handleClickCustom }> <Text>Click Custom</Text> </TouchableOpacity> </View> ); }}// 自定义子组件class CustomTextInput extends Component{ render() { return( <TextInput // 子组件通过props形式暴露自己的引用 ref={ this.props.inputRef } style={{ height: 40 }} placeholder="请输入数据" /> ); }}
阅读全文
0 0
- ReactNative进阶---Refs
- ReactNative进阶---FlatList(一)
- ReactNative进阶---FlatList(二)
- ReactNative进阶---FlatList(三)
- IOS、Android进阶之ReactNative热更新
- ReactNative Flux框架使用 进阶篇
- ReactNative进阶之评分控件的封装
- ReactNative
- ReactNative
- ReactNative
- ReactNative
- ReactNative
- ReactNative进阶之react-native-storage的使用及封装
- React refs
- refs/for/ 与refs/heads/
- [ReactNative]ReactNative学习资源整合
- [Git] 关于refs/for/ 和refs/heads/
- [Git] 关于refs/for/ 和refs/heads/
- [自制操作系统] JOS文件系统详解&支持工作路径&MSH
- CodeForces
- 一步一步来
- arpr
- 解决httpclient上传multipart form-data文件失败
- ReactNative进阶---Refs
- 进程间通信
- JSP Servlet 实现模糊动态查询并分页(拼接Sql)
- apache-comnons系列之commons-digester 3.3.2 学习笔记
- Windows屏保程序思考
- ubuntu linux dpkg 处理软件包,出错: 依赖关系问题
- post-removal 报错
- (二分法)【POJ-3104】Drying
- DBUtils用法详解01