2

In my react project how can I use all the images from a folder?

I tried this :

importAll(r) {

    return r.keys().map(r);
  }
  userPath="../css/pics/"+this.props.user+"/";

  images = this.importAll(require.context("../css/pics/"+this.props.user, false, /\.(png|jpe?g|svg)$/));

But this is giving TypeError: __webpack_require__(...).context is not a function.

I know this question is already asked here : How to import all images from a folder in ReactJS? But it didn't helped me.

Now how can I solve this?

Vasu Ch
  • 185
  • 2
  • 12
  • are you using webpack? this will only work with webpack as the answer mentions. it should be already configured if you're using create-react-app to bootstrap your project – kennyvh Feb 02 '20 at 09:28
  • Yes.I've created app using create-react-app only – Vasu Ch Feb 02 '20 at 11:09
  • @VasuCh You can't actually do that, unfortunately, Webpack doesn't "yet" support [dynamic expressions in import](https://webpack.js.org/api/module-methods/#dynamic-expressions-in-import), it need to be hard-coded like: `../css/pics/john` – awran5 Feb 02 '20 at 15:05
  • But I need to give it dynamically.Is there any other alternatives for that? – Vasu Ch Feb 04 '20 at 17:45

0 Answers0