The issue is you are trying to access a property of a javascript variable without first checking if it is defined. See Detecting an undefined object property
You are also not indexing your array properly and you are abusing the spread operator (...
). There is no reason to spread a number. ...5
has no real semantic meaning.
console.log({...5}); // yeilds {}
That means you are trying to do images[{}]
and means you are trying to index your array with the string [object Object]
. This is probably not what you meant to do. You should just do images[this.state.imageNumber]
.
With regards to your error Cannot read property 'original' of undefined
, I imagine it's likely because you never initialized your state. As a result, this.state.imageNumber
is undefined
. That means you are indexing your array like images[undefined]
. What you should do is either handle the undefined
case or initialize your state to some sensible default.
Something like:
class MyComponent extends React.Component {
state = { imageNumber: 0 };
...
render() { ... }
}
At any rate, I would suggest you consider taking a javascript ramp-up course, as you are confusing fundamentals of javascript with reactjs and this may make it difficult for you to debug problems with either.