I can't setState after getting object from storage
melihmucuk opened this issue · comments
Hi, this code can't update state, how can I change state ?
store.get('user').then((user) => {
this.setState({user: user});
});
You can test with self:
var self = this;
store.get('user').then((user) => {
self.setState({user: user});
});
nothing changed
@melihmucuk Can you verify that the .then
function is being invoked. Since .get
returns a promise there could be an error being thrown, in which case adding a .catch
function might help you debug this.
store.get('user').then((user) => {
this.setState({user: user});
}).catch((error) => {
// discover thrown error here
});
The library doesn't do anything to bind or change context and it should just rely on the ES6 arrow functions lack of context.
Beyond this, I'd need a more complete code sample replicating this. I'm using this library in an app and I am able to .setState
in the .then
function so I know that it is possible.
@melihmucuk I think you want to save an object to the new "state" then in that case your "state" needs to be an object:
this.state = {
user: {}
};
So, for me works using this or self, I think the problem is that "{}".
@melihmucuk I'm going to close this issue for now. If this is still giving you trouble and you can provide a bit larger example of the code I would be happy to revisit it and try and help you get this figured out.