ref属性不只是string 
ref属性不仅接受string类型的参数,而且它还接受一个function作为

callback。这一特性让开发者对ref的使用更加灵活。
render() {
return <TextInput ref={(c) => this._input = c} />;
},
componentDidMount() {
this._input.focus();
},
render(){
return <View ref={ (e) => this._view = e } />//将组件view作为参数赋值给了this._view
}
componentDidMount(){
this._view.style = { backgroundColor:'red',width:100,height:200 }
}

需要提醒大家的是,只有在组件的render方法被调用时,ref才会被调用,组件才会返回ref。如果你在调用this.refs.xx时render方法还没被调用,那么你得到的是undefined。 
心得:ref属性在开发中使用频率很高,使用它你可以获取到任何你想要获取的组件的对象,有个这个对象你就可以灵活地做很多事情,比如:读写对象的变量,甚至调用对象的函数。

让组件做到局部刷新setNativeProps 
有时候我们需要直接改动组件并触发局部的刷新,但不使用state或是props。 
setNativeProps 方法可以理解为web的直接修改dom。使用该方法修改 View 、 Text 等 RN自带的组件 ,则不会触发组件的 componentWillReceiveProps 、 shouldComponentUpdate 、componentWillUpdate 等组件生命周期中的方法。

import React, {
AppRegistry,
Component,
StyleSheet,
Text,
View,
TextInput
} from 'react-native'; class AwesomeProject extends Component {
// 构造
constructor(props) {
super(props);
// 初始状态
this.state = {
textInputValue: ''
};
this.buttonPressed = this.buttonPressed.bind(this);
} buttonPressed() { //当按钮按下的时候执行此函数
let textInputValue = 'new value'; this.setState({textInputValue}); //修改文本输入框的属性值
this.refs.textInputRefer.setNativeProps({
editable:false
}); this.refs.text2.setNativeProps({
style:{
color:'blue',
fontSize:30
}
});
//使文本输入框变为不可编辑
} render() {
return (
//ref={'text2'}> //指定本组件的引用名
<View style={styles.container}>
<Text style={styles.buttonStyle} onPress={this.buttonPressed}>
按我
</Text>
<Text style={styles.textPromptStyle} ref="text2">
文字提示
</Text>
<View>
<TextInput style={styles.textInputStyle}
ref="textInputRefer"
value={this.state.textInputValue}
onChangeText={(textInputValue)=>this.setState({textInputValue})}
/>
</View>
</View>
);
}
} const styles = StyleSheet.create({
container: {
flex: 1,
justifyContent: 'center',
alignItems: 'center'
},
buttonStyle: { //文本组件样式,定义简单的按钮
fontSize: 20,
backgroundColor: 'grey'
},
textPromptStyle: { //文本组件样式
fontSize: 20
},
textInputStyle: { //文本输入组件样式
width: 150,
height: 50,
fontSize: 20,
backgroundColor: 'grey'
}
}); AppRegistry.registerComponent('Redux', () => AwesomeProject);

当点击按钮时,会刷新3个控件的值,但是只是单独去改变,而不是通过改变state状态机的机制来刷新界面,在重复需要多次刷新时使用,普通的时候直接通过state改变即可。

这样用的缺点就是局部改变,回导致状态机混乱。

React Native ref高级用法&&setNativeProps使用的更多相关文章

  1. React之ref详细用法

    在react典型的数据流中,props传递是父子组件交互的唯一方式:通过传递一个新的props值来使子组件重新re-render,从而达到父子组件通信.当然,就像react官网所描述的一样,在reac ...

  2. React中ref的用法

    在React数据流中,父子组件唯一的通信方式是通过props属性:那么如果有些场景需要获取某一个真实的DOM元素来交互,这时候就要用到React的refs属性. 1.可以给DOM元素添加ref属性 c ...

  3. React native 中 SectionList用法

    一.代码 import React, { Component } from 'react'; import { AppRegistry, View, Text, SectionList, } from ...

  4. [RN] 01 - Init: Try a little bit of React Native

    Ref: React Native跨平台移动应用开发 后记:这本书博客味有点浓. 本篇涉及新建工程的若干套路,以及一点语法知识. 创建新工程 (1) 解决的一大核心问题: (2) 使用Javascri ...

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

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

  6. react native中state和ref的使用

    react native中state和ref的使用 因props是只读的,页面中需要交互的情况我们就需要用到state. 一.如何使用state 1:初始化state 第一种方式: construct ...

  7. 你不可不知的 React Native 混合用法(Android 篇)

    前言 当前 React Native 虽说版本更新比较快,各种组件也提供的很全面了,但是在某些情况下,混合开发的方式才会快速缩短开发周期,原因无非就是原生平台的"底蕴"无疑更深,拥 ...

  8. React ref的用法

    React的ref有3种用法: 1. 字符串(已废弃)2. 回调函数3. React.createRef() (React16.3提供) 1. 字符串 最早的ref用法. 1.dom节点上使用,通过t ...

  9. react第七单元(组件的高级用法-组件的组合(children的用法)-高阶组件-封装组件)

    第七单元(组件的高级用法-组件的组合(children的用法)-高阶组件-封装组件) #受控组件 简而言之,就是受到状态state控制的表单,表单的值改变则state值也改变,受控组件必须要搭配onc ...

随机推荐

  1. Docker Ubuntu/CentOS 下运行 java jar

    官方安装方法 https://docs.docker.com/engine/installation/linux/ubuntu/ Ubuntu安装包 https://download.docker.c ...

  2. [转]Javascript的匿名函数

    本文转自:http://dancewithnet.com/2008/05/07/javascript-anonymous-function/ 一.什么是匿名函数? 在Javascript定义一个函数一 ...

  3. Murano Weekly Meeting 2015.09.29

    Meeting time: 2015.September.29th 1:00~2:00 Chairperson:  Serg Melikyan, PTL from Mirantis Meeting s ...

  4. node模块机制

    一.node模块化机制  1.commonJS模块规范包括三部分:模块引用.模块定义.模块标识.例如: //math.js exports.add = function(){    var sum = ...

  5. PHP与redis的操作

    String 类型操作 string是redis最基本的类型,而且string类型是二进制安全的.意思是redis的string可以包含任何数据.比如jpg图片或者序列化的对象   $redis-&g ...

  6. canvas剪辑区域

  7. Java笔记 —— 初始化

    Java笔记 -- 初始化 h2{ color: #4ABCDE; } a{ text-decoration: none !important; } a:hover{ color: red !impo ...

  8. SpringBoot常用应用程序属性

    参考地址: https://docs.spring.io/spring-boot/docs/current/reference/html/common-application-properties.h ...

  9. lunix重启service network restart错误Job for network.service failed. See 'system 或Failed to start LSB: Bring

    1.mac地址不对 通过ip addr查看mac地址,然后修改cd /etc/sysconfig/network-scripts/目录下的文件里面的mac地址 2.通过以下方法 systemctl s ...

  10. SharePoint 2010 列表查阅项栏的formfield控件对象取值

    开发的时候想当然的认为主表解析出来就是一个dropdownlist,可是在大数据测试的时候,发现有情况. 首先创建一个子列表:DetailList,并添加19条数据: 创建主列表:MainList,并 ...