Hello Team,
I am currently facing a challenge with managing state updates in a React component where multiple concurrent API calls are involved. Specifically, I am using the useState
hook to handle formdata
and updating this state based on the results from several parallel API requests. Here is a summary of my approach:
import { JsonForms } from "@jsonforms/react";
import {
materialRenderers,
materialCells,
} from "@jsonforms/material-renderers";
import { Button } from "@mui/material";
import { useState } from "react";
import schema from "./schema.json";
import "./styles.css";
import uischema from "./uischema.json";
export default function App() {
const [formdata, setFormdata] = useState({});
const [errors, setErrors] = useState([]);
const clickHandler = () => {
fetch("https://mocki.io/v1/73bc6c1f-471f-4092-be45-a358321d9c46")
.then((response) => response.json())
.then((json) => {
setFormdata((pre) => ({
...pre,
name1: json.value,
}));
});
fetch("https://mocki.io/v1/73bc6c1f-471f-4092-be45-a358321d9c46")
.then((response) => response.json())
.then((json) => {
setFormdata((pre) => ({
...pre,
name2: json.value,
}));
});
fetch("https://mocki.io/v1/3040136a-0ecc-477b-8251-7f4984508a46")
.then((response) => response.json())
.then((json) => {
setFormdata((pre) => ({
...pre,
name3: json.value,
}));
});
fetch("https://mocki.io/v1/73bc6c1f-471f-4092-be45-a358321d9c46")
.then((response) => response.json())
.then((json) => {
setFormdata((pre) => ({
...pre,
name4: json.value,
}));
});
fetch("https://mocki.io/v1/73bc6c1f-471f-4092-be45-a358321d9c46")
.then((response) => response.json())
.then((json) => {
setFormdata((pre) => ({
...pre,
name5: json.value,
}));
});
fetch("https://mocki.io/v1/73bc6c1f-471f-4092-be45-a358321d9c46")
.then((response) => response.json())
.then((json) => {
setFormdata((pre) => ({
...pre,
name6: json.value,
}));
});
fetch("https://mocki.io/v1/3040136a-0ecc-477b-8251-7f4984508a46")
.then((response) => response.json())
.then((json) => {
setFormdata((pre) => ({
...pre,
name7: json.value,
}));
});
fetch("https://mocki.io/v1/3040136a-0ecc-477b-8251-7f4984508a46")
.then((response) => response.json())
.then((json) => {
setFormdata((pre) => ({
...pre,
name8: json.value,
}));
});
fetch("https://mocki.io/v1/73bc6c1f-471f-4092-be45-a358321d9c46")
.then((response) => response.json())
.then((json) => {
setFormdata((pre) => ({
...pre,
name9: json.value,
}));
});
fetch("https://mocki.io/v1/73bc6c1f-471f-4092-be45-a358321d9c46")
.then((response) => response.json())
.then((json) => {
setFormdata((pre) => ({
...pre,
name10: json.value,
}));
});
fetch("https://mocki.io/v1/73bc6c1f-471f-4092-be45-a358321d9c46")
.then((response) => response.json())
.then((json) => {
setFormdata((pre) => ({
...pre,
name11: json.value,
}));
});
};
return (
<div>
<div className="container">
<JsonForms
data={formdata}
renderers={materialRenderers}
cells={materialCells}
onChange={({ data, errors }) => {
setFormdata(data);
setErrors(errors);
}}
schema={schema}
uischema={uischema}
/>
<Button
variant="contained"
sx={{ marginTop: "10px" }}
onClick={clickHandler}
>
Call API
</Button>
</div>
</div>
);
}
In this setup, each API request updates a different field within formdata
, leading to frequent re-renders of the component and noticeable flickering in the UI.
Note: Due to constraints within our project structure, I am unable to use Promise.all
or await/Promise chaining to wait until all API requests resolve. Each API call must update the state independently as soon as it completes.
Question: Is there an efficient method to batch state updates or otherwise optimize the handling of concurrent API responses to minimize UI flickering and excessive re-rendering? I am seeking best practices or alternative approaches that ensure the component updates with each API response while mitigating the issue of frequent re-renders.
Thank you for your assistance.