ReactNative自定义控件状态更新的正确姿势
来源:互联网 发布:数据库冗余 编辑:程序博客网 时间:2024/06/08 05:05
ReactNative自定义控件状态更新的正确姿势
我们经常要自定义一个ReactNative控件,而在Render()方法中会用到组件的属性,而这些属性不能即时更新,导致界面不能刷新。举例如下:
比如我们定义了一个按钮组件,这个组件外部会指定一个颜色,我们在构造方法中初始化当前的属性borderColor为this.props.borderColor;
'use strict'import React, {Component} from 'react';import { View, Text, TouchableOpacity} from 'react-native';export default class Button extends Component { constructor(props) { super(props); this.state = { borderColor : this.props.borderColor || "#FF0000" } } componentWillReceiveProps(nextProps) { if(nextProps.borderColor != this.borderColor){ this.setState({ borderColor : nextProps.borderColor }); } } render() { return (<TouchableOpacity onPress = { this.props.onPress }> <View style={ [{ borderWidth: 1, borderColor: this.state.borderColor, justifyContent:"center", }, this.props.style]}> <Text style = { { textAlign : "center", textAlignVertical: "center" , alignSelf:"center",flex:1} } > { this.props.text } </Text> </View> </TouchableOpacity>); }}
阅读全文
0 0
- ReactNative自定义控件状态更新的正确姿势
- 使用自定义控件的正确姿势
- 编写自定义控件构造函数的正确姿势 - defStyleAttr/defStyleRes
- 缓存更新的正确姿势
- 手动更新gradle正确姿势?
- ReactNative自定义控件。
- 自定义view的正确姿势-教你一步步画出笑脸
- 自定义注解设置缓存有效期的正确姿势
- 搜索的正确姿势
- 读开源库的正确姿势
- 跑步的正确姿势
- 跑步的正确姿势
- 正确的关机姿势
- 正确的科研姿势
- ReactNative自定义控件之 RefreshLayout
- 电脑前的正确姿势
- 二分查找的正确姿势
- 集成sdk的正确姿势
- Android判断应用是否正在前台运行&Activity是否正在前台运行
- EPEL源
- android 性能测试工具
- apache的rewrite一直重定向,但找不到原因
- 【转】数据库基本知识:(十)数据操作 · 查 · (三)使用子查询访问和修改数据
- ReactNative自定义控件状态更新的正确姿势
- JZOJ5426. 【NOIP2017提高A组集训10.25】摘Galo
- mac 安装win7 攻略
- eclipse 配置pydev插件
- Windows 下批量生成 SWF 缩略图的快速解决方案
- 年龄
- Glide源码解析之山清水秀疑无路(一)
- 理解 LSTM 网络
- Portia可视化爬虫部署