我是 react-native 的新手,我只是想尝试处理动作和状态。我不明白这个组件的问题在哪里。
我收到错误 undefined is not an object (evaluating 'this.wrappedInstance.setNativeProps')
import React, { Component } from 'react';
import { Container, Text } from 'native-base';
import {
AppRegistry,
View,
TouchableHighlight
} from 'react-native';
export default class Page2 extends Component {
constructor(){
super();
this.state = {
mess: "Page 2 message"
}
}
onPress(){
this.state.mess = this.state.mess+" wow a click!"
}
render() {
return (
<View>
<TouchableHighlight
onPress={this.onPress}
underlayColor="blue"
>
<Text> {this.state.mess}</Text>
</TouchableHighlight>
</View>
);
}
}
AppRegistry.registerComponent('Page2', () => Page2);
你永远不想改变状态,相反你会想要使用 setState。
onPress(){
this.state.mess = this.state.mess+" wow a click!" //mutating state
}
改成:
onPress = () => { //arrow syntax will change the scope of `this` to your component
this.setState(prevState => ({ mess: prevState.mess + " wow a click!" });
} // anytime you want to alter state based on previous state you should use this syntax
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句