loading json data from local file into React JS

前端 未结 8 1761
无人及你
无人及你 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 00:58

    My JSON file name: terrifcalculatordata.json

    [
        {
          "id": 1,
          "name": "Vigo",
          "picture": "./static/images/vigo.png",
          "charges": "PKR 100 per excess km"
        },
        {
          "id": 2,
          "name": "Mercedes",
          "picture": "./static/images/Marcedes.jpg",
          "charges": "PKR 200 per excess km"
        },
        {
            "id": 3,
            "name": "Lexus",
            "picture": "./static/images/Lexus.jpg",
            "charges": "PKR 150 per excess km"
          }
    ]
    

    First , import on top:

    import calculatorData from "../static/data/terrifcalculatordata.json";
    

    then after return:

      
    { calculatorData.map((calculatedata, index) => { return (

    {calculatedata.charges}

提交回复
热议问题