我希望通过这段代码实现点一下text+1,但是没有成功



  • var info=1;
    var AwesomeProject = React.createClass({
    _onPressButton: function(){
    info=2;
    },
    render: function() {
    return (

    <View style={styles.container}>
        <Text style={styles.teststyle}>
            {info}
        </Text>
        <TouchableHighlight onPress={this._onPressButton} >
            <Image source={require('./images/p1.jpg')}/>
        </TouchableHighlight>
    </View>
    
    );
    

    },
    });

    自问自答小能手

    0


  • 额,原来是需要用state才能实现啊
    var AwesomeProject = React.createClass({
    getInitialState: function() {
    return {info: 1};
    },
    _onPressButton: function(event){
    var temp=this.state.info+1;
    this.setState({info:temp});
    },
    render: function() {
    var text=this.state.info;
    return (
    <View style={styles.container}>
    <Text style={styles.teststyle}>
    {text}
    </Text>
    <TouchableHighlight onPress={this._onPressButton} >
    <Image source={require('./images/p1.jpg')}/>
    </TouchableHighlight>
    </View>

    );
    

    },
    });

    自问自答小能手

    0

登录后回复