The context : I try to build a form with hooks and render component depending on the step we are.
Problem : I have this message : "Error: UserFormHooks(...): Nothing was returned from render. This usually means a return statement is missing. Or, to render nothing, return null."
I know the code work with class but no with hooks.
What I tried : I tried to put them in a ternary but it doesn't work either.
Code :
import React, { useState } from "react";
import FormUserDetails from "./FormUserDetails";
import FormPersonalDetails from "./FormPersonalDetails";
import Confirm from "./Confirm";
import Success from "./Success";
const UserFormHooks = () => {
const [data, setData] = useState({
step: 1,
firstName: "",
lastName: "",
email: "",
occupation: "",
city: "",
bio: "",
});
// Proceed to next step
const nextStep = () => {
setData(step + 1);
};
// Go back to prev step
const prevStep = () => {
setData(step - 1);
};
// Handle fields change
const handleChange = (input) => (e) => {
setData({ [input]: e.target.value });
};
const { step } = { data };
const { firstName, lastName, email, occupation, city, bio } = { data };
const values = { firstName, lastName, email, occupation, city, bio };
switch (step) {
case 1:
return (
<div>
<FormUserDetails
nextStep={nextStep}
handleChange={handleChange}
values={values}
/>
</div>
);
case 2:
return (
<div>
<FormPersonalDetails
nextStep={nextStep}
prevStep={prevStep}
handleChange={handleChange}
values={values}
/>
</div>
);
case 3:
return (
<Confirm nextStep={nextStep} prevStep={prevStep} values={values} />
);
case 4:
return <Success />;
default:
console.log("This is a multi-step form built with React.");
}
};
export default UserFormHooks;