# React-Native 学习笔记之组件化开发和setState的使用

**URL:** <https://www.shiqidu.com/t/topic/171>\
**Category:** 开发调优\
**Tags:** React-Native\
**Created:** [2017 年4 月 18 日 16:48 UTC](https://www.shiqidu.com/t/topic/171 "2017-04-18T16:48:57Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

作者： ![Leon](https://www.shiqidu.com/user_avatar/www.shiqidu.com/leon/32/5_2.png) [@Leon](https://www.shiqidu.com/u/Leon)\
发布日期： [2017 年4 月 18 日 16:48 UTC](https://www.shiqidu.com/t/topic/171/1 "2017-04-18T16:48:57Z")

</div>

推荐一个在线react native模拟器，可直接执行rn代码  
[http://dabbott.github.io/react-native-web-player](http://dabbott.github.io/react-native-web-player "http://dabbott.github.io/react-native-web-player")

* * *

学了一个晚上，语法很不熟悉，因为一个`setState`多了个`=`（当成成员变量了）排了半天错，下面的代码主要讲rn模块化开发的简单的例子，和属性`state`及方法`setState`的使用。

```javascript
import React,{Component} from 'react';
import {
    AppRegistry,
    StyleSheet,
    Text,
    View,
    Image,
    TouchableOpacity
} from 'react-native';

const nameList = ['zhangsan','lisi','wangwu','zhaoliu'];
class GoodMorning extends Component{
    static defaultProps = {
        name : 'somebody'
    };
    render(){
        return (
            <Text>good morning,{this.props.name}!</Text>
        )
    }
}

const GoodEvening = (props) => {
    return(
        <Text>good evening,{props.name}!</Text>
    )
};

class shiqidu extends Component{
    state = {
        likes : 0,
    };
    onPress = ()=>{
        const {likes} = this.state;
        const newLikes = likes + 1;
        this.setState({
            likes : newLikes
        });
    };
    render(){
        return(
            <View style={{marginTop:20,backgroundColor:'#D5D5D5'}}>
                <GoodEvening name="luzhaung" />
                {
                    nameList.map((name,index)=>{
                        return <GoodMorning key={index} name={name} />
                    })
                }
                <View style={styles.container}>
                    <TouchableOpacity onPress={this.onPress}>
                        <Image
                            style={styles.images}
                            source={{
                                uri:'https://github.com/facebook/react-native/blob/master/Examples/UIExplorer/js/Thumbnails/like.png?raw=true'
                            }} />
                    </TouchableOpacity>
                    <Text>{this.state.likes}</Text>
                </View>
            </View>
        )
    }
}

const styles = StyleSheet.create({
    container:{
        flex:1,
        justifyContent:'center',
        alignItems:'center',
        backgroundColor:'#d5d5d5',
    },
    images:{
        width:65,
        height:65
    }
});
AppRegistry.registerComponent('shiqidu', () => shiqidu);

```

执行结果  
 ![点赞数量在模拟器上没显示，坑。。](https://discourse.shiqidu.com/uploads_nj/article/2017-04-19/58f644a50dde6.png "点赞数量在模拟器上没显示，坑。。")

---

<div class="post-metadata">

作者： ![xiaoqingnian](https://www.shiqidu.com/user_avatar/www.shiqidu.com/xiaoqingnian/32/483_2.png) [@xiaoqingnian](https://www.shiqidu.com/u/xiaoqingnian)\
发布日期： [2017 年4 月 19 日 02:07 UTC](https://www.shiqidu.com/t/topic/171/2 "2017-04-19T02:07:14Z")

</div>

厉害了我的骚！

---

<div class="post-metadata">

作者： ![Leon](https://www.shiqidu.com/user_avatar/www.shiqidu.com/leon/32/5_2.png) [@Leon](https://www.shiqidu.com/u/Leon)\
发布日期： [2017 年4 月 20 日 06:00 UTC](https://www.shiqidu.com/t/topic/171/3 "2017-04-20T06:00:34Z")

</div>

[em\_13]，学学吧。
