React Native 快速入门之认识Props和State
2016-03-09 09:41
806 查看
眼下React Native(以后简称RN)越来越火,我也要投入到学习当中。对于一个前端来说,还是有些难度。因为本人觉得这是一个App开发的领域,自然是不同。编写本文的时候,RN的版本为0.21.0。我们马上以代码进入今天的学习。
简要分析:
所谓props,就是属性传递,而且是单向的。
属性多的时候,可以传递一个对象,语法为{...xx},这是es6的新特性。
React靠一个state来维护状态,当state发生变化则更新DOM。
今天到此为止,下次见。
'use strict'; import React, { AppRegistry, Component, StyleSheet, Text, View } from 'react-native'; class Hello extends Component { render() { return ( <View> <Text>{ this.props.title}</Text> <Text>{ this.props.text}</Text> </View> ); } } class HelloComponent extends React.Component{ constructor (props) { super(props); this.state = { appendText: '' }; } _setText() { this.setState({appendText: ' Native!'}); } render() { return ( <View> <Text onPress={this._setText.bind(this)}> {this.props.text + this.state.appendText} </Text> </View> ); } } class learn01 extends Component { render() { const pros = { text: 'hi', title: 'title' } return ( <View> <View style={{height:30}} /> <Hello {...pros} /> <HelloComponent text="React"/> </View> ); } }
简要分析:
所谓props,就是属性传递,而且是单向的。
属性多的时候,可以传递一个对象,语法为{...xx},这是es6的新特性。
React靠一个state来维护状态,当state发生变化则更新DOM。
今天到此为止,下次见。
相关文章推荐
- [RxJS] Reactive Programming - Sharing network requests with shareReplay()
- [RxJS] Reactive Programming - Clear data while loading with RxJS startWith()
- windows7搭建react native开发环境
- ReactiveCocoa 学习心得 -- 1
- [RxJS] Reactive Programming - New requests from refresh clicks -- merge()
- MVVM-ReactiveCocoa
- 【无源汇有上下界可行流】ACdream1211-Reactor Cooling
- react与native异步交互的几种方式
- ACE反应器(Reactor)模式(4)
- ACE反应器(Reactor)模式(3)
- ACE反应器(Reactor)模式(2)
- ACE反应器(Reactor)模式(1)
- React Native(第二节点击事件)
- React Native开发之按钮的使用
- ReactiveCocoa入门教程——第二部分
- ReactiveCocoa入门教程——第一部分
- React-约束组件及无约束组件详解
- 源码推荐(03.08):ReactiveCocoa登录交互效果的实现,可编辑可拖动排序
- [RxJS] Reactive Programming - Rendering on the DOM with RxJS
- [RxJS] Reactive Programming - Why choose RxJS?