Reputation: 57
I'm trying to kick off undefined, but I don't understand, what I'm doing wrong. I need to initialize persons without undefined
import { useEffect, useState } from "react";
import { SearchBar } from "./SearchBar";
import { getPersons } from "../../apies/api";
import { ListOfPersons } from "./listOfPersons";
const Home = () => {
const [valueOfInput, valueOfInputChange] = useState("")
const [persons, setPersons] = useState()
useEffect(() => {
getPersons().then(response => setPersons(response))
}, [setPersons]);
console.log(persons)
return <div>
<h1>Поиск персонажей</h1>
<SearchBar personGetRequest={valueOfInputChange}/>
<ListOfPersons personsList={persons}/>
</div>;
};
export default Home;
Upvotes: 1
Views: 1220
Reputation: 488
I would suggest you to:
count
,next
,previous
,result
fields), in order to call the function setPersons with the result
import { useEffect, useState } from "react";
import { SearchBar } from "./SearchBar";
import { getPersons } from "../../apies/api";
import { ListOfPersons } from "./listOfPersons";
const Home = () => {
const [valueOfInput, valueOfInputChange] = useState("")
const [persons, setPersons] = useState([])
useEffect(() => {
getPersons()
.then(response => setPersons(response.data.results))
.catch(e => setPersons([])
}, []);
useEffect(()=>console.log(persons),[persons])
return <div>
<h1>Поиск персонажей</h1>
<SearchBar personGetRequest={valueOfInputChange}/>
<ListOfPersons personsList={persons}/>
</div>;
};
export default Home;
Upvotes: 1
Reputation: 363
You should initialize the persons with empty array then it will not show undefined. Like the below code
const [persons, setPersons] = useState([])
Upvotes: 2
Reputation: 77
You Should use this for fetching data
useEffect(() => {
getPersons().then(response => setPersons(response.data.data))
}, [setPersons]);
console.log(persons)
Upvotes: 0