J
J
jslby2019-07-02 13:57:09
JavaScript
jslby, 2019-07-02 13:57:09

How to dynamically connect components in ReactJS?

There is this code:

import React, {useState, useEffect} from 'react';
import { Container } from 'react-grid-system';

import Header from '../../ui/header';
import Footer from '../../ui/footer';

export default (props) => {
  const DynPage = require('./' + props.name);
  return(
    <div>
      <Header />
      <Container>
        <DynPage />
      </Container>
      <Footer />
    </div>
  )
}

This is the base, a few more effects will be added, but even in this state it does not function.
It writes this error:
Warning: React.createElement: type is invalid -- expected a string (for built-in components) or a class/function (for composite components) but got: object.

Uncaught Invariant Violation: Element type is invalid: expected a string (for built-in components) or a class/function (for composite components) but got: object.

From the code the idea is clear, but I will try to explain. I want to load "stupid" components from one component (index.js) that lie side by side. They are all identical, and they all need the same functionality, only the text changes. That is why I took out only the text in third-party files.
The code works if you execute import beforehand and then create something like ac. an array where all the components are listed, but it's very dirty, I would like it this way. In the future I will wrap it in try...catch
PS props.name is passed from the router, everything is ok with it

Answer the question

In order to leave comments, you need to log in

2 answer(s)
A
Anton Spirin, 2019-07-02
@rockon404

Code split

D
davidnum95, 2019-07-02
@davidnum95

have you looked at react-loadable ?

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question