I have the following example code:
export function SomeFormComponent() {
const [alertMessage, setAlertMessage] = useState("");
const [alertTitle, setAlertTitle] = useState("");
const [alertSeverity, setAlertSeverity] = useState("");
//to be called at the result of form result
const setAlert = (title, message, severity) => {
setAlertTitle(title);
setAlertMessage(message);
setAlertSeverity(severity);
}
return (<div>
<FormContents/>
<Alert severity={alertSeverity}>
<AlertTitle>{alertTitle}</AlertTitle>
{alertMessage}
</Alert>
</div>)
}
It is a form with an alert component, where the result of a fetch is shown. My problem is that I duplicate this code (useState(), setAlert(), ) in each form that my application has. I want to know if there a way to do this without duplicating code.