Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

189
Views
How do I render the react components dynamically from imported module?

I am trying to render the React components dynamically based on the routes that are given in the URL.

canvas/index.tsx

export { default as CardGroup } from "./cards.example";

src/App.tsx

import React from "react";
import {
  useParams
} from "react-router-dom";
import * as Canvas from "./canvas";


const App: React.FC = () => {
    const params = useParams();
    const widgetName = (params.widgetName ?? '';
      return (
      <div className = "App" > 
      {< Canvas[widgetName] / >} // error here
      </div>
      );
    };

export default App;

I am not able to render the widget. It give me the following error.

Element implicitly has an 'any' type because expression of type 'string' can't be used to index type 'typeof import("/Users/mypc/Code/atoms/src/canvas/index")'.

No index signature with a parameter of type 'string' was found on type 'typeof import("/Users/mypc/Code/atoms/src/canvas/index")'.

enter image description here

about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

Canvas[widgetName] isn't a valid React component. Compute the component value first as a local variable and then render it as JSX.

I suggest creating a local CanvasMap object so you can type it correctly for what is imported. You are creating a lookup object that uses string type keys and returns the React.FC component.

Example:

import React from "react";
import * as Canvas from "./canvas";
import { useParams } from "react-router-dom";

const CanvasMap: { [K: string]: React.FC<{}> } = {
  ...Canvas
};

const App: React.FC = () => {
  const { widgetName } = useParams();
  const CanvasComponent = CanvasMap[widgetName ?? "NotFound"];
  return (
    <div className="App">
      <CanvasComponent />
    </div>
  );
};

export default App;
about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!