0
votes

Hi I am making a website building app that uses styled components and forms to render data into sections of a page. So I decided all of my styled components will be default exports then imported to a context and then loaded into the form when its opened. Then from a select menu the user gets a map of all the possible components to render and when an option is selected a string reference filters the component array down to the called function and then adds all the other data and then displays it on screen. When I launch I get this error: "React.jsx: type is invalid -- expected a string (for built-in components) or a class/function (for composite components) but got: < BurgerMenu / >. Did you accidentally export a JSX literal instead of a component?"

Here are all of the parts:

BurgerMenu.js

import React, { useState, useRef } from "react";
import { ThemeProvider } from "styled-components";
import { useOnClickOutside } from "../../state/useOnClickOutside";
import { GlobalStyles } from "../../state/globals";
import { useTheme } from "../../state/useTheme";
import Hamburger from "./Hamburger";
import Menu from "./Menu";
const BurgerMenu = () => {
  const [open, setOpen] = useState(false);
  const node = useRef();
  useOnClickOutside(node, () => setOpen(false));
  const { theme } = useTheme();
  return (
    <ThemeProvider theme={theme}>
      <>
        <GlobalStyles />
        <div ref={node}>
          <Hamburger open={open} setOpen={setOpen} />
          <Menu open={open} setOpen={setOpen} />
        </div>
      </>
    </ThemeProvider>
  );
};
export default BurgerMenu;

Component Context:

import BurgerMenu from "../header/Hamburger/BurgerMenu";
 const components = [{
      name: "BurgerMenu",
      func: BurgerMenu,
      els: ["a", "a", "a", "a", "a", "a"],
    }]

Site Form (where the component context is called)

const { components } = useComponentContext();
const [mount, setMount] = useState('')
<select name='component' onChange={(e) => setMount(e.target.value)}>
    {components.map((component) => (
          <option key={component.name} value={component.name}>
             {component.name}
           </option>
      ))}
</select>
<button
   className='btn primary btn-block'
   onClick={() => {
       convertStringToComponent(mount);
       setTimeout(setLoaded((prevState) => !prevState),2000)}}>
              Add Component
</button>

Function convertStringToComponent

const convertStringToComponent = (mount, compStyle) => {
    const ComponentName = components
      .filter((comp) => comp.name === mount)
      .map(({ func }) => func)[0];

    return (
      <ComponentName
        content={content}
        font={font}
        pallet={pallet}
        h={h}
        icon={icon}
        p={p}
        vid={vid}
        img={img}
        a={a}
        li={li}
        button={button}></ComponentName>
    );
  };

const VariableComponent = convertStringToComponent(mount);

this is then called in a different component with

 {loaded === true && <VariableComponent />}

Any help would be great!

1

1 Answers

1
votes

The issue is with how you are using convertStringToComponent's function reeturn valaue.

When you call, convertStringToComponent you are returned an instance of component which is

<ComponentName
    content={content}
    font={font}
    pallet={pallet}
    h={h}
    icon={icon}
    p={p}
    vid={vid}
    img={img}
    a={a}
    li={li}
    button={button}></ComponentName>

Now when rendering you are trying to again create an instance eout of this by using it as

{loaded === true && <VariableComponent />}

instead of

 {loaded === true && VariableComponent}

However, there is another issue, when you call convertStringToComponent you shouldn't try to store the result in a variable and export it, instead you should be storing it in instance state and render it.

A better way to structure your code would be

export const convertStringToComponent = (mount, compStyle) => {
    const ComponentName = components
      .filter((comp) => comp.name === mount)
      .map(({ func }) => func)[0];
    
    return  (otherProps) => (
      <ComponentName
        {...otherProps}
        content={content}
        font={font}
        pallet={pallet}
        h={h}
        icon={icon}
        p={p}
        vid={vid}
        img={img}
        a={a}
        li={li}
        button={button}></ComponentName>
    );
  };

Now you can use it like

const [VariableComponent, setVariableComponent] = useState(null);

<button
   className='btn primary btn-block'
   onClick={() => {
       const comp = convertStringToComponent(mount);
       setVariableComponent(comp);
       setTimeout(setLoaded((prevState) => !prevState),2000)}}>
              Add Component
</button>


{isLoaded && <VariableComponent />}