在 React-Native 中访问 this.state
Accessing this.state in React-Native
嗨,我是 React 的新手,请多多包涵。我想将地理位置存储为一个状态。看起来很漂亮,因为位置的任何变化都会触发渲染,这正是我想要的。在开发过程中,我有一个按钮,通过访问 lastPosition
手动触发事件。但是当我这样做的时候。状态是"undefined"。知道为什么吗?
export default class FetchProject extends Component {
constructor(props) {
super(props);
this.state = {
initialPosition: 'unknown',
lastPosition: 'unknown',
};
}
//code that sets lastposition
componentDidMount() {
....
}
_onPressGET (){
console.log("PressGET -> " + this.state); //undefined
var northing=this.state.initialPosition.coords.latitude;
//triggers error
}
render() {
return (
<View style={styles.container}>
<TouchableHighlight onPress = {this._onPressGET} style = {styles.button}>
<Text>Fetch mailbox</Text>
</TouchableHighlight>
</View>
);
}
}
在 RN 中使用 ES6 类 时,注意绑定 this
- this
除非你绑定它,否则可能不是你想的那样。
onPress = { this._onPressGet.bind(this) }
或者在构造函数中
constructor(props) {
super(props);
this.state = {
initialPosition: 'unknown',
lastPosition: 'unknown'
};
this._onPressGet = this._onPressGet.bind(this);
}
或者也许是最优雅的方式
_onPressGet = () => {
// Function body
}
从低到高排列。
引自React Docs:
You have to be careful about the meaning of this in JSX callbacks. In JavaScript, class methods are not bound by default. If you forget to bind this.handleClick
and pass it to onClick
, this will be undefined
when the function is actually called.
This is not React-specific behavior; it is a part of how functions work in JavaScript. Generally, if you refer to a method without ()
after it, such as onClick={this.handleClick}
, you should bind that method.
嗨,我是 React 的新手,请多多包涵。我想将地理位置存储为一个状态。看起来很漂亮,因为位置的任何变化都会触发渲染,这正是我想要的。在开发过程中,我有一个按钮,通过访问 lastPosition
手动触发事件。但是当我这样做的时候。状态是"undefined"。知道为什么吗?
export default class FetchProject extends Component {
constructor(props) {
super(props);
this.state = {
initialPosition: 'unknown',
lastPosition: 'unknown',
};
}
//code that sets lastposition
componentDidMount() {
....
}
_onPressGET (){
console.log("PressGET -> " + this.state); //undefined
var northing=this.state.initialPosition.coords.latitude;
//triggers error
}
render() {
return (
<View style={styles.container}>
<TouchableHighlight onPress = {this._onPressGET} style = {styles.button}>
<Text>Fetch mailbox</Text>
</TouchableHighlight>
</View>
);
}
}
在 RN 中使用 ES6 类 时,注意绑定 this
- this
除非你绑定它,否则可能不是你想的那样。
onPress = { this._onPressGet.bind(this) }
或者在构造函数中
constructor(props) {
super(props);
this.state = {
initialPosition: 'unknown',
lastPosition: 'unknown'
};
this._onPressGet = this._onPressGet.bind(this);
}
或者也许是最优雅的方式
_onPressGet = () => {
// Function body
}
从低到高排列。
引自React Docs:
You have to be careful about the meaning of this in JSX callbacks. In JavaScript, class methods are not bound by default. If you forget to bind
this.handleClick
and pass it toonClick
, this will beundefined
when the function is actually called.This is not React-specific behavior; it is a part of how functions work in JavaScript. Generally, if you refer to a method without
()
after it, such asonClick={this.handleClick}
, you should bind that method.