so i have this logic where user click a button then based on the clicked button different component get rendered I was wondering if there is a better method to implement this logic
function Test(props) {
const [component, setComponent] = useState("createPatient")
const [rendered, setRendered] = useState()
function clickHandler(component) {
switch (component) {
case "createPatient":
setRendered(<PatientForm/>)
break
case "findPatient":
setRendered(<FindPatient/>)
break
case "patientsList":
setRendered(<PatientListTable/>)
}
}
return (
<div>
<button onClick={() => clickHandler("createPatient")}>Create Patient</button>
<button onClick={() => clickHandler("findPatient")}>Find Patient</button>
<button onClick={() => clickHandler("patientsList")}>Patients List</button>
{ rendered }
</div>
);
}
export default Test;
All the other answers here are correct and work well for your use case. I would like to take it a step further and introduce you to code-splitting. It is not necessary but you can always try it.
Presently, you are loading all the components you need at once when your Test component is loaded. If your imported components in the future get big or complex, the initial rendering can be slowed down.
Code splitting allows you to split your code into small chunks which you can then load on demand.
Here is a working sandbox.
import React, { useState } from "react";
function Test() {
const [component, setComponent] = useState(null);
const LoadComponent = async location => {
const { default: component } = await import(`./components/${location}`);
setComponent(component);
};
return (
<div>
<h1>Click the button to proceed</h1>
<button onClick={() => LoadComponent("PatientForm")}>
Create Patient
</button>
<button onClick={() => LoadComponent("FindPatient")}>
Find Patient
</button>
<button onClick={() => LoadComponent("PatientListTable")}>
Patients List
</button>
{component}
</div>
);
}
export default Test;
If you are using the latest version of React, you can use React.lazy. You'll also need to wrap this lazy component with Suspense. You can modify the above code like this to load components with React.lazy.
import React, { useState, Suspense } from "react";
const LoadComponent = location => {
const Component = React.lazy(() => import(`./components/${location}`));
setComponent(<Component />);
};
return <Suspense fallback={<div>Loading...</div>}>{component}</Suspense>
Always think in terms of how you can ship the least amount of JavaScript to the user.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With