I have a lot of apis that are INDEPENDENT of each other and needs to be stored in the React state before it is rendered. I have fetch
calls in componentDidMount
but I am not sure what is the best way to approach this.
Should I...
1. have nested fetch calls
example:
componentDidMount() {
fetch('url')
.then((res) => res.json())
.then((data) => {
// call another fetch in here
})
}
OR 2. have separate fetch calls
example:
componentDidMount() {
fetch('url')
.then((res) => res.json())
.then((data) => {
// set state in here
})
// call another fetch for the other url endpoint
fetch('url2')
.then((res) => res.json())
.then((data) => {
// set state in here
})
}
I'm not sure if one way is considered better practice over the other, but I would love to know what you guys think and what some of the pros/cons would be.
UPDATE: I am using Promise.all() now, but I am getting the Promise returned rather than the actual value. Here is my code:
Promise.all([
fetch(`/api/url1`),
fetch(`/api/url2`),
])
.then(([res1, res2]) => (
{
res1: res1.json(),
res2: res2.json(),
}))
.then(({res1, res2}) => {
this.setState({
state1: res1,
state2: res2,
});
})
.catch((error) => {
console.log(error);
});
When i checked the values of my state on the console, here is what I get:
Promise
__proto__: Promise
[[PromiseStatus]]: "resolved"
[[PromiseValue]]: Array(6530)
Any idea of what I may be missing / doing wrong?
Thanks!
How many times componentDidMount is called? React components call componentDidMount only once by default. You can run the component multiple times if you delete the component or change the props or state.
Putting your API call code in componentDidMount will ensure that data is only fetched from the client, where it should be.
The componentDidMount() method allows us to execute the React code when the component is already placed in the DOM (Document Object Model). This method is called during the Mounting phase of the React Life-cycle i.e after the component is rendered.
componentDidMount isn't deprecated and is definitely still safe to use, so there's no need to add UNSAFE_ to that method.
As far as fetch
returns Promise
you can use Promise.all
function to execute both fetches in parallel:
componentDidMount() {
Promise.all([fetch('url'), fetch('url2')])
.then(([res1, res2]) => {
return Promise.all([res1.json(), res2.json()])
})
.then(([res1, res2]) => {
// set state in here
});
}
As @CertainPerformance mentioned, you should use Promise.all
method.
Promise.all([
fetch("url1"),
fetch("url2"),
fetch("url3"),
]).then(([res1, res2, res3]) => {
this.setState({status: "fetched"})
})
const url = "https://asdqwe.free.beeceptor.com/my/api/path";
const promises = Promise.all([
fetch(url),
fetch(url),
fetch(url),
]);
promises
.then((results) =>
Promise.all(results.map(r => r.text()))
)
.then(console.log)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With