Import JSON file in React

前端 未结 11 1043
遥遥无期
遥遥无期 2020-11-28 06:05

I\'m new to React and I\'m trying to import a JSON DATA variable from an external file. I\'m getting the following error:

Cannot find mo

11条回答
  •  挽巷
    挽巷 (楼主)
    2020-11-28 06:50

    This worked well in React 16.11.0

    // in customData.js
    export const customData = {
      //json data here
      name: 'John Smith',
      imgURL: 'http://lorempixel.com/100/100/',
      hobbyList: ['coding', 'writing', 'skiing']
    }
    
    // in index.js
    import { customData } from './customData';
    
    // example usage later in index.js
    

    {customData.name}

提交回复
热议问题