I am working on building a large form application. The issue is that the form consists of many nested forms. The first stage/ form is similar for all users. Then, depending on the user's input, a different form will show up. Meaning each form has its own local state, and upon submitting each and every form the global state is being updated.
for clarification see a small part of the global state below:
const [state, setState] = useState({
startForm {
email: "",
priority: "",
requestType: "",
dueDate: "",
},
designRequest: {
designType: "",
yellow: { client:"" , message: "" },
red: { change: "", parameter: "" }
}
}
);
const handleStartStepData = (startStepData) => { var startStep = {...state.startForm} state.startForm = startStepData setState({ startStep })
};
return ( <StartForm state={startStepState} onChange={handleStartStepData}/>
)
start form:
export const StartForm = (props) => {
const methods = useForm();
const [state, setState] = useState({
email: "",
priority: "",
requestType: "",
dueDate: "",
});
const onSubmit = (data) => {
const startFormState = data;
props.onChange(startFormState);
};
after filling out the start form (email, priority,requestType, and due date), and depending on the requestType a different form will show up on the screen. So if for example, the user chose a design request, they will then choose between a yellow and red request. I am using react forms hook along with material UI components. I tried setting up a handle submit for each small form on the parent(global form) but each of them overrides the global state instead of merging it. I am a newbie in react and could use any idea you have on better managing the state in this application.