Skip to content

Instantly share code, notes, and snippets.

@yinkakun
Created November 9, 2023 13:49
Show Gist options
  • Save yinkakun/8c2a1936553abb3b64e247b986a056c2 to your computer and use it in GitHub Desktop.
Save yinkakun/8c2a1936553abb3b64e247b986a056c2 to your computer and use it in GitHub Desktop.
Render a component from component object
import React from "react";
type Steps = "component-a" | "component-b" | "component-c";
const ComponentA = () => {
return <div>Component B</div>;
};
const ComponentB = () => {
return <div>Component B</div>;
};
const ComponentC = () => {
return <div>Component C</div>;
};
const STEPS: Record<Steps, React.FC> = {
"component-a": ComponentA,
"component-b": ComponentB,
"component-c": ComponentC,
};
const Form = () => {
const currentStep = "component-a"; // state might be from url search param
const CurrentStep = STEPS[currentStep];
return <CurrentStep />;
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment