Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Switch case to render multiple components on JSX

I’m conditionally rendering some components, but I would like to know if there is a more efficient way (for exemple, with switch case):

     export const Culture = () => {
      const { query } = useRouter();
      const currentSection = query.section || "";
      return (
        <div className="content">
          <Navigation
            className={currentSection !== "" ? "sub_nav" : "sub_nav active_menu"}
            category="culture"
          />
          {currentSection === "" && <CultureCulture category="cultureCulture" />}
          {currentSection === "elephant" && (
            <CultureCulture category="cultureElephant" />
          )}
          {(currentSection === "espace-musee" ||
            currentSection === "quebecor-gallery") && (
            <CultureCulture category="cultureGallery" />
          )}
          {currentSection === "culture-d-ici" && (
            <CultureCulture category="cultureIci" />
          )}
          {(currentSection === "bilan-culturel" ||
            currentSection === "culture-report") && (
            <CultureCulture category="cultureReport" />
          )}
        </div>
      );
    };
    export default Culture;

>Solution :

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

You can actually use switch-case blocks in a separate function to render them.

export const Culture = () => {
    const { query } = useRouter();
    const currentSection = query.section || "";

    const renderCurrentSelection = () => {
        switch (currentSection) {
            case "":
                return <CultureCulture category="cultureCulture" />;
            case "elephant":
                return <CultureCulture category="cultureElephant" />;
            case "espace-musee":
            case "quebecor-gallery":
                return <CultureCulture category="cultureGallery" />;
            case "culture-d-ici":
                return <CultureCulture category="cultureIci" />;
            case "bilan-culturel":
            case "culture-report":
                return <CultureCulture category="cultureReport" />;
            default:
                return null;
        }
    };

    return (
        <div className="content">
            <Navigation
                className={
                    currentSection !== "" ? "sub_nav" : "sub_nav active_menu"
                }
                category="culture"
            />
            {renderCurrentSelection()}
        </div>
    );
};
export default Culture;
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading