R
Reactiflux

✅ – Alforoan – 03-36 Jan 4

✅ – Alforoan – 03-36 Jan 4

AAlforoan1/4/2023
if an api link gives me only one page of a collection of movies and i want multiple pages how do i combine the data? i can get 2nd,3rd, 4th, etc. pages by changing the link at the end to page=(number)
Solution:
```js const [currentPage, setCurrentPage] = useState(1); const FetchMovies = async (page) => { const response = await fetch(url+page);...
Jump to solution
Gghardin1371/4/2023
do you want to grab them all at once or when needed?
AAlforoan1/4/2023
well actually i suppose i can just copy paste all of them into a data file, right i wanted to grab them all at once
Gghardin1371/4/2023
do you know ahead of time how many pages there are?
AAlforoan1/4/2023
💀
Gghardin1371/4/2023
yeah you probably don't want to load all of that at once
AAlforoan1/4/2023
i was thinking add a load more button at the bottom
Gghardin1371/4/2023
what would you use it all at once for?
AAlforoan1/4/2023
ive never done it so i didnt know what u were asking for lol
Gghardin1371/4/2023
if you're trying to do a load more you'd just append the data to your state when you fetch it
AAlforoan1/4/2023
so make multiple state hooks?
Gghardin1371/4/2023
nah just one
const MyList = () => {
const [list, setList] = useState([]);
const [currentPage, setCurrentPage] = useState(1);

useEffect(() => {
fetch(url).then(data => setList(previous => [...previous, ...data] ));
}, [currentPage])

const loadMore = () => {
setCurrentPage(previous => previous + 1);
}

.....
}
const MyList = () => {
const [list, setList] = useState([]);
const [currentPage, setCurrentPage] = useState(1);

useEffect(() => {
fetch(url).then(data => setList(previous => [...previous, ...data] ));
}, [currentPage])

const loadMore = () => {
setCurrentPage(previous => previous + 1);
}

.....
}
AAlforoan1/4/2023
for the fetching part, whats the diff btwn urs and something like this
const FetchMovies = async () => {
const response = await fetch(url);
const movies = await response.json();
setMovies(movies.results);
};

React.useEffect(() => {
FetchMovies();
}, []);
const FetchMovies = async () => {
const response = await fetch(url);
const movies = await response.json();
setMovies(movies.results);
};

React.useEffect(() => {
FetchMovies();
}, []);
Gghardin1371/4/2023
i'm appending the data to the end of the list array whereas you're replacing the array but you could do it like this
Solution
Gghardin1371/4/2023
const [currentPage, setCurrentPage] = useState(1);

const FetchMovies = async (page) => {
const response = await fetch(url+page);
const movies = await response.json();
setMovies(prev => [...prev, ...movies.results]);
};

React.useEffect(() => {
FetchMovies(currentPage);
}, [currentPage]);
const [currentPage, setCurrentPage] = useState(1);

const FetchMovies = async (page) => {
const response = await fetch(url+page);
const movies = await response.json();
setMovies(prev => [...prev, ...movies.results]);
};

React.useEffect(() => {
FetchMovies(currentPage);
}, [currentPage]);
AAlforoan1/4/2023
ok, that's helpful. ty 🙂
UUUnknown User1/4/2023
3 Messages Not Public
Sign In & Join Server To View

Looking for more? Join the community!

R
Reactiflux

✅ – Alforoan – 03-36 Jan 4

Join Server