3

I would like to dynamically import a module from a path importPath set via Props.

var importPath;

class MainComponent extends Component {
    state = {}
    render() {
        // Set var importPath = "path_to_module here;"
        // importPath = this.props.myModulePath
        return (
            <ComponentToImport myComponentPath="./ToastExample" />);
    }
}

export default MainComponent;

Then :

class ComponentToImport extends Component {
    ToastExample: (async () => {
        await import (this.props.revPath)
    })()

    async sayHiFromJava() {
        this.state.ToastExample.showJ('Awesome', ToastExample.SHORT);
    }

    render() {
        return (<ToastExample />);
    }
}

How can I go about this?

Thank you all in advance.

How do I attach ToastExample in import ToastExample from importPath; to await import("importPath"); so that I can return(<ToastExample />);

UPDATE

I have tried :

class ComponentToImport extends Component {
    ToastExample: (async () => {
        await import (this.props.revPath)
    })()

    async sayHiFromJava() {
        this.state.ToastExample.showJ('Awesome', ToastExample.SHORT);
    }

    render() {
        return (<ToastExample />);
    }
}

but I get the error :

error: bundling failed: index.js: index.js:Invalid call at line 28: import(_this.props.myComponentPath)

4 Answers 4

3

I guess this is the way:

import("importPath").then(() => {
    // your code
});

or

await import("importPath");

// your code

see more here https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/import

Sign up to request clarification or add additional context in comments.

2 Comments

How do I attach ToastExample in import ToastExample from importPath;'' to await import("importPath");` so that I can return(<ToastExample />);
I tried that but I get an error: bundling failed: index.js: index.js:Invalid call at line 28: import(_this.props.revPath). Any suggestions? I did an update to the question.
3

Is it what your are looking for?

const ToastExample = await import('importPath');

EDIT: Please read the official doc to set up your webpack or Babel (https://reactjs.org/docs/code-splitting.html)

class ComponentToImport extends Component {
  constructor(props) {
    super(props);
    this.state = { module: null };
  }

  componentDidMount() {
    const { path } = this.props;
    import(`${path}`).then(module => this.setState({ module: module.default }));
  }
  render() {
    const { module: Component } = this.state;
    return <div>{Component && <Component />}</div>;
  }
}

3 Comments

I tried that but I get an error: bundling failed: index.js: index.js:Invalid call at line 28: import(_this.props.revPath). Any suggestions? I did an update to the question.
Thank you again for the reply, but how do I attach ToastExample in import ToastExample from importPath; to await import("importPath"); so that I can return(<ToastExample />);
@Program-Me-Rev by passing the "<ToastExample />" path to this component, you are returning your "<ToastExample />" but it is named "<Component />".
0

There are 2 options

  1. Using React.lazy and <Suspense /> works both CSR and SSR.
  2. I wrote a small hook that imports an external module on CSR. You can check this github example on the usage & explaination.

Comments

-2

If you want to pass component to child component one way is to pass through child props.

import myComponentPath from "./ToastExample"
<ComponentToImport>
  <myComponentPath />
<ComponentToImport/>

and then

class ComponentToImport extends Component {
    render() {
        return (this.props.children);
    }
}

May be this helps.

Thanks

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.