Answer the question
In order to leave comments, you need to log in
How does this code work with axios?
There was a question about using the component.
An obscure piece of code from there:
loadThumbnails = () => {
const { page, limit } = this.state;
this.setState({
isLoading: true,
isError: false,
}, () => {
axios.get(
`api/somePath?limit=${limit}&page=${page + 1}`
).then(response => {
const { data: { elements, total } } = response;
this.setState(prevState => ({
elements: [
...prevState.elements,
...elements
],
page: page + 1,
isLoading: false,
total,
}));
}).catch(error => {
this.setState({
isLoading: false,
isError: true,
});
});
});
};
Answer the question
In order to leave comments, you need to log in
I then threw off this example to you to show a case of using this component that is close to life. Everything doesn't have to be done that way.
1. Look, your component implements an endless loading of information from the server. But how are you going to implement the interaction between the client and the server? How can the server find out how much data you received and which ones to give next? The simplest and time-tested pagination option . You need to use a paginator on the server side.
Let's say there are 500 images on the server. How to request the first 20?
The server should return something like:
{
data: [...],
quantity: 500,
}
const arr1 = ['a'];
const arr2 = ['b'];
const arr3 = [...arr1, ...arr2];
const arr4 = [...arr1, 1, ...arr2, 'c'];
console.log(arr3);
// => ['a', 'b'];
console.log(arr4);
// => ['a', 1, 'b', 'c'];
const obj1 = { a: 'a', b: 'old b' };
const obj2 = { b: 'new b', c: 'c' };
const obj3 = { ...obj1, ...obj2 };
const obj4 = { ...obj2, ...obj1 };
const obj5 = { ...obj1, ...obj2, d: 'd' };
console.log(obj3);
// => { a: 'a', b: 'new b', c: 'c' };
console.log(obj4);
// => { a: 'a', b: 'old b', c: 'c' };
console.log(obj5);
// => { a: 'a', b: 'new b', c: 'c', d: 'd' };
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question