Skip to content
Advertisement

Can’t set state in js react-native

Getting error while trying to setState in React Native.

Code

JavaScript

Error

TypeError: undefined is not an object (evaluating ‘_this.setState’)

Advertisement

Answer

useState Hook

Functional components don’t have access to setState method but useState hook.

useState hook works by defining the name of value, e.g. foo followed by it’s setter. It’s a convention to name the setter with the same name as that of value with set prefix, i.e. setFoo

JavaScript

Solution

JavaScript
User contributions licensed under: CC BY-SA
5 People found this is helpful
Advertisement