if you are new to react dont bump into react hooks at first . read and follow some react introduction posts on this platform and by the way setState is not a react function .
yes thanks for useeffect, Now I wanted to pass this person to customers state variable but it is rendering as empty which is exactly why I have not used useEffect in the first place {"data":[]}
import React from "react";
import { useState, useEffect } from "react";
// import "./styles.css";
export default function App() {
const [person, setPerson] = useState([]);
useEffect(() => {
fetch("https://jsonplaceholder.typicode.com/users")
.then((response) => {
return response.json();
})
.then((data) => {
setPerson(data); // You Need this For set Person to `state person`
});
}, []);
console.log("dasdasd", person);
const [customers, setCustomers] = useState({ data: person });
return <div className="App">{JSON.stringify(customers)}</div>;
}
You can check for data !== null && return (...) for conditional rendering. Since it will rerender after data loaded, it would also prevent from empty rendering.
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.
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
if you are new to react dont bump into react hooks at first . read and follow some react introduction posts on this platform and by the way setState is not a react function .
working code
hey, when I ran this code, it is repeatedly hitting the API and never stopping. I want it to run only once
If you only want to run useEffect once , you can give it an empty array as second argument.
yes thanks for useeffect, Now I wanted to pass this person to customers state variable but it is rendering as empty which is exactly why I have not used
useEffectin the first place{"data":[]}You can check for data !== null && return (...) for conditional rendering. Since it will rerender after data loaded, it would also prevent from empty rendering.
It is being empty forever, not re-rendering at all.
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.
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
You still need setPerson. Basically with a functional component using hooks, setState is not defined. Hence your type error.