React - Dynamically Import Components

前端 未结 8 1685
别那么骄傲
别那么骄傲 2020-12-05 07:14

I have a page which renders different components based on user input. At the moment, I have hard coded the imports for each component as shown below:

    imp         


        
8条回答
  •  盖世英雄少女心
    2020-12-05 08:01

    import React, { Component } from 'react'
    import Component1 from './Component1'
    import Component2 from './Component2'
    import Component3 from './Component3'
    
    class Main extends Component {
        render() {
            var type = 'Component1';  // just an example
            return (
              
    {type == "Component1" && } {type == "Component2" && } ...
    ) } } export default Main

    You can use conditional rendering insted. Hope it will help

    Check this

提交回复
热议问题