I created an app that is a clone of flickr. As the user type in the input box it should render the pictures associated with the search value. Unfortunately, when I type it tells me "Cannot read property 'photo' of undefined", but then it kinda works. I really would like this app to work without any bugs. Please see attached the codesandbox.
1 Answer
Make this function like this. First check weather photo is present.
jsonp(flickrURL, flickrParams, { callback: "jsoncallback" }).then(
results => {
console.log(results);
if(results.photos){
const images = _(results.photos.photo).map(generateURL);
console.log(images);
this.setState({ images: images });
}
}
);
const images = _(results).map(generateURL);this will solve your problemthis.state.queryto the parent even before state.query is set to the actual value. setState sets the state at later point of time. so try callingthis.props.onInput(event.target.value);