0

I want to render different Components based on some checkboxes selection pattern without having to import components that may not be used.

I have an Array which contains the Component names (I used numbers as an example) and I want to import each component based on the values of the array.

I came up with something like this:


import {Suspense} from 'react'


export default function CreationForm() {

 const docs = [1,3,5]

  return (
      <Suspense fallback={<div>Loading...</div>}>
        {
          docs.map(val => React.lazy(() => import(`documents/${val}.jsx`)))   
        }
      </Suspense>
  )

}

I know this solution does not work but I think it explains what I am trying to do.

I could try using state but the actual "docs array" is an state variable in the real application so it could cause duplicated state.

I did this as a test and worked:

  const A = React.lazy(() => import(`documents/1.jsx`))
  ...
  *** SNIP ***
  ...
      <Suspense fallback={<div>Loading...</div>}>
        {
          docs.map((val) => <A/>)   
        }
      </Suspense>

But I cannot dynamically import each component like this.

4
  • Why is it important that you don't import Components that aren't used? Are you trying to keep a super low code footprint? That comes with some tough challenges. Commented Sep 5, 2022 at 11:42
  • The amount of Components I have to import could vary on the future and I don't see it viable to import them every time I add one. Commented Sep 5, 2022 at 11:45
  • Either I don't understand what you're trying to do, or you don't understand what import does. Typically your IDE will help in managing your imports. You also don't typically import conditionally (or at all). You might render conditionally, but the imports are static. Commented Sep 5, 2022 at 11:47
  • I have my doubts if what I am trying to do is the best solution for the problem I'm facing. What I'm intending to do is render a new component (that are all different kinds of forms) based on some checkboxes. If a particular checkbox is selected it may require to render one form (or maybe more). Commented Sep 5, 2022 at 11:51

1 Answer 1

1

Ok, so you don't need conditional imports, you just want to do conditional rendering. That's waaaaay simpler.

Example:

import { FormA } from "./FormA";
import { FormB } from "./FormB";

const MyComponent = ({ which }) => {
    return <>
        {which === "form-a" && <FormA />}
        {which === "form-b" && <FormB />}
    <>;
};
Sign up to request clarification or add additional context in comments.

4 Comments

I thought of this as a solution. You think using an object as alternative could be much less efficient or less readable for others?
Use an object where?
Instead of using the short-circuit returning the component as: return FORMS[which]
Sure, you could do that. There aren't really any rules on when something should be a Component.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.