How to access event.nativeEvent.layout.x in react native /w expo

1.1k Views Asked by At

I'm simply trying to access the x coordinate through the onLayout prop within a TouchableOpacity component in React Native and I'm getting an error in the emulator:

null is not an object (evaluating 'event.nativeEvent.layout')

This is my first time dabbling into the React animation side of things and have no idea why it's failing. There also doesn't seem to be much online about it so I'm starting to think it's been deprecated.

<TouchableOpacity 
   onLayout={(event) => this.setState(prevState => ({ 
       tabs: { ...prevState.tabs, xTabOne: event.nativeEvent.layout.x } 
   }))}
/>
1

There are 1 best solutions below

0
On

So interestingly when I remove the prevState part of the setState the value can be found so I'm assuming this is some scope issue with the set state function.