I have a problem with react, if I fetch the same information from an API and that information is repeated, I have to create a lot of variables to hold every single one of the information.
So I repeat like const img1 and const img2 and three to hold all that l want from the same thing l just repeat it. How can l do it better?
setDisplayName(response.data.feed["0"].display.displayName);
setIngSteps(response.data.feed["0"].content.preparationSteps);
setImg(response.data.feed["0"].display.images["0"]);
setDisplayName2(response.data.feed["1"].display.displayName)
setIngSteps2(response.data.feed["1"].content.preparationSteps);
setImg2(response.data.feed["1"].display.images["0"]);
setDisplayName3(response.data.feed["1"].display.displayName)
setIngSteps3(response.data.feed["1"].content.preparationSteps);
setImg3(response.data.feed["1"].display.images["0"]);
Is there another simpler way ?
There's no rule that says a state value needs to be a single value. Any object can be stored in state.
So just store the object in state:
setData(response.data);
Then you can retrieve the values from that state object as needed.