Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Render React element from object value

I want to create an object:

import React from "react";
import { Registration } from "../../";

const RouteObj = {
  Registration: {
    route: "/registration",
    comp: <Registration />
  }
};
export default RouteObj;

And then, in a second file call:

import React from 'react';
import RouteObj from ...

class Thing extends React.Component{
  render() {
    <RouteObj.Registration.comp />
  }
}

When trying this, I get the error:

React.createElement: type is invalid -- expected a string (for built-in components) or a class/function (for composite components) but got: undefined. You likely forgot to export your component from the file it's defined in.

Is it possible to render React components in this way?

like image 941
Ben Irving Avatar asked May 26 '26 01:05

Ben Irving


1 Answers

Registration is already a tag (a component), that's why I guess you have to use curly brackets instead.

import React from 'react';
import RouteObj from ...

class Thing extends React.Component{
  render() {
    <div>
      {RouteObj.Registration.comp}
    </div>
  }
}
like image 90
kind user Avatar answered May 27 '26 14:05

kind user