react native fun() fun=()=>的区别

单独的时候这两个都没有问题,就是如果在react native 中运行的时候,第一个会无法访问this.state.mycount变量,原因是函数需要bind的,如果没有这样,那么需要使用fun=()=>这个函数,如果bind了,就没有问题

onPress={() => this._onPress(rowData)}>这个就是没有用到this._onPress.bind(this),而是用的fun=()=>{},我的上一篇文章说的bind的问题

_renderRow(rowData) {
    return <TouchableHighlight
        underlayColor='#008b8b'
        onPress={() => this._onPress(rowData)}>
        <View style={[styles.rowStyle, {flexDirection: 'row', justifyContent: 'space-between'}]}>
            <View style={{flex: 1}}>
                <Text style={styles.rowText}>{rowData.title}</Text>
                <View style={{flexDirection: 'row', marginTop: 10, justifyContent: 'space-between', alignItems: 'flex-end'}}>
                    <View style={{flexDirection: 'row', alignItems: 'center'}}>
                        <Image
                            style={{width: 20, height: 20, borderRadius: 10, marginRight: 5}}
                            source={{uri: rowData.user.avatarLarge}}
                        />
                        <Text style={styles.other}>{rowData.user.username}</Text>
                        <Text style={styles.other}>{Moment(rowData.createAt).fromNow()}</Text>
                    </View>
                    <Text style={[styles.other, {}]}>{rowData.commentsCount} 个回复</Text>
                </View>
            </View>
            {
                rowData.screenshot ? <Image style={{width: 60}} source={{uri: rowData.screenshot}}/> : <View/>
            }
        </View>
    </TouchableHighlight>
}

猜你喜欢

转载自blog.csdn.net/fivestar2009/article/details/82384242
今日推荐