admin管理员组文章数量:1317572
My project is using react, redux, and redux-thunk.
I want to wait for my function to end before launching a function that clear my page. Unofortunately i have a problem accessing my function in .then
Here was my previous code without promise and then :
this.props.dispatch(ScheduleAction(..))
this.props.deleteTab()
the problem was that sometime deleteTab() is called before sending the info to my server so not very good.
then i did :
Promise.resolve(this.props.dispatch(ScheduleAction(..)))
.then(function(response) {
this.props.deleteTab();
console.log("TabDeleted !!!");
return response
})
The problem now is that i can't access this.props.deleteTab();
And i have this as error :
Uncaught (in promise) TypeError: Cannot read property 'props' of undefined
If anyone have an idea on how to fix that by accessing props in .then, thanks in advance !!
My project is using react, redux, and redux-thunk.
I want to wait for my function to end before launching a function that clear my page. Unofortunately i have a problem accessing my function in .then
Here was my previous code without promise and then :
this.props.dispatch(ScheduleAction(..))
this.props.deleteTab()
the problem was that sometime deleteTab() is called before sending the info to my server so not very good.
then i did :
Promise.resolve(this.props.dispatch(ScheduleAction(..)))
.then(function(response) {
this.props.deleteTab();
console.log("TabDeleted !!!");
return response
})
The problem now is that i can't access this.props.deleteTab();
And i have this as error :
Uncaught (in promise) TypeError: Cannot read property 'props' of undefined
If anyone have an idea on how to fix that by accessing props in .then, thanks in advance !!
Share Improve this question asked Apr 19, 2018 at 10:25 foufrixfoufrix 1,4664 gold badges22 silver badges49 bronze badges 1-
have you tried of defining
this.props
outside of yourpromise
? Or you can also passthis.props
as parameter in your.then()
function parameter. – Nah Commented Apr 19, 2018 at 10:32
3 Answers
Reset to default 10Try this. You don't have access to this inside then. If you use arrow methods as below, you should be able to access it.
Promise.resolve(this.props.dispatch(ScheduleAction(..)))
.then((response)=> {
this.props.deleteTab();
console.log("TabDeleted !!!");
return response
})
The problem you have here is that you lost the context of this
keyword, which means this
now is undefined.
Since you can access this.props
outside the promise, what you want is to bind
this
inside your promise
function by doing smth like this
Promise.resolve(this.props.dispatch(ScheduleAction(..)))
.bind(this)
.then(function(response) {
this.props.deleteTab();
console.log("TabDeleted !!!");
return response
})
hope this method work for you
const $this = this;
const { dispatch } = this.props;
Promise.resolve(dispatch(ScheduleAction(..)))
.then(function(response) {
$this.deleteTab();
console.log("TabDeleted !!!");
return response;
})
本文标签: javascriptAccessing props in then (Reactredux)Stack Overflow
版权声明:本文标题:javascript - Accessing props in .then (Reactredux) - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1742009449a2412611.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论