L
L
lexstile2018-10-26 13:46:12
React
lexstile, 2018-10-26 13:46:12

How to work with json files in react js?

Created a simple application.
You need to make a request to json and get the data:

fetch('/data.json')
        .then(function(response) {
          return response.json()
        }).then(function(json) {
          console.log('parsed json', json)
        }).catch(function(ex) {
          console.log('parsing failed', ex)
        });

What do I need to do to open the file? (now returns 404 error and can't be opened via direct link).
package.json
{
  "name": "app",
  "version": "1.0.0",
  "description": "",
  "main": "index.js",
  "scripts": {
    "start": "webpack-dev-server --config ./webpack.config.js --mode development --open --hot",
    "build": "webpack --mode production"
  },
  "keywords": [],
  "author": "",
  "license": "ISC",
  "devDependencies": {
    "@babel/core": "^7.1.2",
    "@babel/preset-env": "^7.1.0",
    "@babel/preset-react": "^7.0.0",
    "autoprefixer": "^9.3.1",
    "babel-loader": "^8.0.4",
    "babel-plugin-transform-class-properties": "^6.24.1",
    "css-loader": "^1.0.0",
    "html-webpack-plugin": "^3.2.0",
    "less": "^3.8.1",
    "less-loader": "^4.1.0",
    "postcss-cli": "^6.0.1",
    "postcss-loader": "^3.0.0",
    "style-loader": "^0.23.1",
    "webpack": "^4.22.0",
    "webpack-cli": "^3.1.2",
    "webpack-dev-server": "^3.1.9"
  },
  "dependencies": {
    "react": "^16.5.2",
    "react-dom": "^16.5.2"
  }
}

Answer the question

In order to leave comments, you need to log in

1 answer(s)
R
Roman Alexandrovich, 2018-10-26
@RomReed

https://codesandbox.io/s/817126rxmj

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question