loading json data from local file into React JS

前端 未结 8 1644
无人及你
无人及你 2020-11-30 00:33

I have a React component and I want to load in my JSON data from a file. The console log currently doesn\'t work, even though I\'m creating the variable data

相关标签:
8条回答
  • 2020-11-30 01:00

    You could add your JSON file as an external using webpack config. Then you can load up that json in any of your react modules.

    Take a look at this answer

    0 讨论(0)
  • 2020-11-30 01:05
    1. install json-loader:

      npm i json-loader --save

    2. create data folder in src:

      mkdir data

    3. put your file(s) there

    4. load your file

      var data = require('json!../data/yourfile.json');

    0 讨论(0)
提交回复
热议问题