DEV Community

Discussion on: setState in Reactjs is not a function

Collapse
 
emmanuelthecoder profile image
Emmanuel Aiyenigba

No. It won't work that way. setState() cannot be called with React hooks.
const [person, setPerson] = useState([])
In the state hook method, the second parameter (in this case setPerson) is what is used to change/update the state.
Now instead of setState(data), do setPerson(data) and it should work just fine.

Collapse
 
saiavinashiitr profile image
Sai Avinash Duddupudi

The reason I am not using setPerson is because it has be wrapped under useEffect hook but I want to pass this person data to another useState Variables.

Since useState happens in initial load, if we use useEffect I wont be able to pass data to useState variables

Collapse
 
sirseanofloxley profile image
Sean Allin Newell

You still need setPerson. Basically with a functional component using hooks, setState is not defined. Hence your type error.