在承诺解析后修改组件状态
Modify component state after a promise resolve
我想在承诺被解析后修改组件的状态(react native)
下面是我的代码: class Greeting extends Component{
constructor(props){
super(props);
this.state = {text: 'Starting...'};
var handler = new RequestHandler();
handler.login('email','password')
.then(function(resp){
this.setState({text:resp});
});
}
render(){
return (
<Text style={this.props.style}>
Resp: {this.state.text}
</Text>
);
}
}
但是当promise解析时,会抛出以下错误:
this.setState is not a function
TypeError: this.setState is not a function
at http://localhost:8081/index.android.bundle?platform=android&dev=true&hot=false&minify=false:1510:6
at tryCallOne (http://localhost:8081/index.android.bundle?platform=android&dev=true&hot=false&minify=false:25187:8)
at http://localhost:8081/index.android.bundle?platform=android&dev=true&hot=false&minify=false:25273:9
at JSTimersExecution.callbacks.(anonymous function) (http://localhost:8081/index.android.bundle?platform=android&dev=true&hot=false&minify=false:8848:13)
at Object.callTimer (http://localhost:8081/index.android.bundle?platform=android&dev=true&hot=false&minify=false:8487:1)
at Object.callImmediatesPass (http://localhost:8081/index.android.bundle?platform=android&dev=true&hot=false&minify=false:8586:19)
at Object.callImmediates (http://localhost:8081/index.android.bundle?platform=android&dev=true&hot=false&minify=false:8601:25)
at http://localhost:8081/index.android.bundle?platform=android&dev=true&hot=false&minify=false:7395:43
at guard (http://localhost:8081/index.android.bundle?platform=android&dev=true&hot=false&minify=false:7288:1)
at MessageQueue.__callImmediates (http://localhost:8081/index.android.bundle?platform=android&dev=true&hot=false&minify=false:7395:1)
如何在承诺被解决后改变当前的组件状态?
回调具有与您正在处理的对象不同的上下文。因此,this
不是你想的那样。
要解决这个问题,您可以使用箭头函数,它保留周围的上下文:
constructor(props){
super(props);
this.state = {text: 'Starting...'};
var handler = new RequestHandler();
handler.login('email','password')
.then(resp => this.setState({text:resp}));
}
或者,使用bind()
手动设置函数上下文:
constructor(props){
super(props);
this.state = {text: 'Starting...'};
var handler = new RequestHandler();
handler.login('email','password')
.then(function(resp){
this.setState({text:resp});
}.bind(this));
}
最好使用新的语法。更多信息请点击https://babeljs.io/blog/2015/06/07/react-on-es6-plus
class Greeting extends Component{
state = {
text: 'Starting...',
}
componentDidMount() {
var handler = new RequestHandler();
handler.login('email','password')
.then((text) => {
this.setState({ text });
})
}
render(){
return (
<Text style={this.props.style}>
Resp: {this.state.text}
</Text>
);
}
}
相关文章:
- React重新渲染但未显示正确的组件
- 不能从angular2中的子组件指定父组件中的数组
- 拨打'父亲'函数形式a'儿童'ReactJS中的组件
- 在Safari执行javascript之前对其进行修改
- 通过emberJS中的控制器修改组件的属性
- Reactjs:在修改对象之前,是否需要复制处于组件状态的对象
- react如何与修改html的javascript ui组件协同工作
- Mithril - 在子组件修改父组件数据后重新排序子组件数组
- 修改角度网格组件格式导出列
- 渲染后修改 React 子组件道具
- 修改bower组件JavaScript文件的最佳实践是什么
- ADF Web应用程序:如何在页面加载时添加和修改UI组件(在页面显示之前)
- 如何创建可以被多个angular2组件修改的对象
- 在承诺解析后修改组件状态
- 当组件'子组件内部发生变化时,修改组件'的视图
- ReactJS-添加/修改无状态功能组件的propTypes的多种方法
- AngularJS组件可以修改它们的包装元素吗?
- 修改在
中定义并在<使用>中使用的 SVG 组件 - 如何修改子组件内容
- 修改非子react组件的状态数据