2

I'm writing a react app, with mobx and framework7, and using code splitting in some of my imports, and I haven't found anything on this, but i want to know if there's a way for me to use a variable inside a import, something like this code:

const Slider = import("../parts/Slider").then( Slider =>
    <Slider>
      {
        //sources would be an array of urls
        sources.map( (source, i,) =>
          <div className="slider" key={i}  style={{position:'relative'}}>
            <img className="picture" src={source} alt=""/>
          </div>
        )
      }
    </Slider>
<script src="https://cdnjs.cloudflare.com/ajax/libs/react/16.4.1/umd/react.production.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/react-dom/16.4.1/umd/react-dom.production.min.js"></script>

1 Answer 1

2

You should just use normal import and pass parameters to your React component. Why not to do something like:

import Slider from "../parts/Slider";

const SliderComp = ( {sources}, ) =>
    <Slider>
      {
        //sources would be an array of urls
        sources.map( (source, i,) => 
          <div className="slider" key={i}  style={{position:'relative'}}>
            <img className="picture" src={source} alt=""/>
          </div>
        )
      }
    </Slider>

export default SliderComp;

and to use it in other file:

import Slider from './SliderComp'
...
<Slider sources={arrayOfSources} />
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks for you answer! I was trying to avoid static imports, but i suppose it won't improve that much performance for this case.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.