N
N
NarkoMan012021-05-13 08:19:34
JavaScript
NarkoMan01, 2021-05-13 08:19:34

How to make React dynamic import?

Good afternoon!
I'm trying to make a dynamic import in React.

/*
Переменная откуда я беру путь до компонента
*/
const steps = [
    {
        name: 'Шаг 1',
        component: './steps/step1.jsx'
    },
    {
        name: 'Шаг 2',
        component: './steps/step2.jsx'
    }
]

/*
Динамический импорт 
*/
 const Step =  React.lazy(()=> import(steps[0].component))

<React.Suspense fallback={<div>Loading...</div>}>
                <Step/>
            </React.Suspense>

That I receive an error that the module will not find.
If you insert a line, then everything works
/*
При таком варианте все работает
*/
 const Step =  React.lazy(()=> import('./steps/step1.jsx'))


Please tell me what is the reason?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
Alexander, 2021-05-13
@NarkoMan01

componentRecord immediately React.lazy.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question