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?
_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}}/>}}
/>
);
}


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
Most helpful comment
Hi !
This issue is not related to this module. It is because your
_renderActionsdoes not have the right context (this). if you console.logthisinside _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 :
You can also use the arrow function syntax for your class methods in order to bind automatically.