React-native-gifted-chat: TypeError: Cannot read property 'setState' of undefined

Created on 22 Jan 2018  Â·  2Comments  Â·  Source: FaridSafi/react-native-gifted-chat

Issue Description

I use NativeModules to choice photo.but when I got the photo,use this.setState({source:image.path}) to change the photo,but throw a warning
TypeError: Cannot read property 'setState' of undefined
It seems to be the scope of action?
so what should I do?

Steps to Reproduce / Code Snippets

 _renderActions(props) {

      const options = {

        'Camera': (props) => {

          ImagePicker.openCamera({
              compressImageQuality : 0.8
          }).then(image => {
            this.setState({
                  source : image.path
              })
          });
        },
        'Photo': (props) => {

          ImagePicker.openPicker({
              compressImageQuality : 0.8,
              smartAlbums : ['UserLibrary','PhotoStream','Bursts']
          }).then(image => {
              this.setState({
                  source : image.path
              })
          });
        },
        'Cancel': () => {},
      };
      return (
        <Actions
          {...props}
          options={options}
          icon={()=>{return <Image source={{uri:'chat_photo',width:30,height:30}}/>}}
        />
      );
    }

Expected Results

image
image

Additional Information

  • Nodejs version: v8.9.1
  • React version: 16.0.0-alpha.12
  • React Native version: 0.48.0
  • react-native-gifted-chat version: 0.3.0
  • Platform(s) (iOS, Android, or both?): iOS

Most helpful comment

Hi !

This issue is not related to this module. It is because your _renderActions does not have the right context (this). if you console.log this inside _renderActions you will have what is available on the global space (such as fetch, alert, ...) but not your component.

A solution will be to simply bind your _renderActions function to your component context

try to add this on the constructor :

class YourClass extends Component {
   constructor() {
     super();
     this._renderActions = this._renderActions.bind(this);
  }

You can also use the arrow function syntax for your class methods in order to bind automatically.

All 2 comments

Hi !

This issue is not related to this module. It is because your _renderActions does not have the right context (this). if you console.log this inside _renderActions you will have what is available on the global space (such as fetch, alert, ...) but not your component.

A solution will be to simply bind your _renderActions function to your component context

try to add this on the constructor :

class YourClass extends Component {
   constructor() {
     super();
     this._renderActions = this._renderActions.bind(this);
  }

You can also use the arrow function syntax for your class methods in order to bind automatically.

I've solved this problem. Thank you. @MehdiAlouafi

Was this page helpful?
0 / 5 - 0 ratings

Related issues

tafelito picture tafelito  Â·  3Comments

arayaryoma picture arayaryoma  Â·  3Comments

cassioseffrin picture cassioseffrin  Â·  3Comments

Fr33maan picture Fr33maan  Â·  3Comments

Hayko1994 picture Hayko1994  Â·  3Comments