Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

this.setState no effect #7

Closed
booxood opened this issue Jul 7, 2015 · 2 comments
Closed

this.setState no effect #7

booxood opened this issue Jul 7, 2015 · 2 comments

Comments

@booxood
Copy link

booxood commented Jul 7, 2015

In the function UIImagePickerManager.showImagePicker, use this.setState set some state, no effect...

Code like :

  componentDidMount: function() {
    var self = this;
    UIImagePickerManager.showImagePicker({title: 'select...'}, function (type, response) {
      console.log(self.state.imageSource);
      self.setState({imageSource: {uri: response}});
      console.log(self.state.imageSource);  // no effect
    });
  },

React native version 0.6.

@marcshilling
Copy link
Contributor

While this.setState is typically very very fast, it's still an async method. It has an optional second argument which is a function that is called after the state is set. Try logging your state like so:

  componentDidMount: function() {
    var self = this;
    UIImagePickerManager.showImagePicker({title: 'select...'}, function (type, response) {
      console.log(self.state.imageSource);
      self.setState({imageSource: {uri: response}}, () => {
         console.log(self.state.imageSource);
      });
    });
  }

You should see the correct logging with that. Also, try using the imageSource in an <Image> in your render method and you'll see the picture!

@booxood
Copy link
Author

booxood commented Jul 8, 2015

😂 My mistake...

@marcshilling You are right. Thank you very much.

@booxood booxood closed this as completed Jul 8, 2015
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants