Accepted answer

You are opening an asynchronous connection, yet you have written your code as if it was synchronous. The reqListener callback function will not execute synchronously with your code (that is, before React.createClass), but only after your entire snippet has run, and the response has been received from your remote location.

Unless you are on a zero-latency quantum-entanglement connection, this is well after all your statements have run. For example, to log the received data, you would:

function reqListener(e) {
    data = JSON.parse(this.responseText);

I'm not seeing the use of data in the React component, so I can only suggest this theoretically: why not update your component in the callback?


If you want to load the file, as part of your app functionality, then the best approach would be to include and reference to that file.

Another approach is to ask for the file, and load it during runtime. This can be done with the FileAPI. There is also another StackOverflow answer about using it: How to open a local disk file with Javascript?

I will include a slightly modified version for using it in React:

class App extends React.Component {
  constructor(props) {
    this.state = {
      data: null
    this.handleFileSelect = this.handleFileSelect.bind(this);

  displayData(content) {
    this.setState({data: content});

  handleFileSelect(evt) {
    let files =;
    if (!files.length) {
      alert('No file select');
    let file = files[0];
    let that = this;
    let reader = new FileReader();
    reader.onload = function(e) {

  render() {
    const data =;
    return (
        <input type="file" onChange={this.handleFileSelect}/>
        { data && <p> {data} </p> }


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


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:

  {, index) => {
        return (
            <div key={index}>


If you have couple of json files:

import data from 'sample.json';

If you were to dynamically load one of the many json file, you might have to use a fetch instead:

  .then(response => response.json())
  .then(data => console.log(data))


  1. Install json-loader:
npm i json-loader --save
  1. Create data folder in src:
mkdir data
  1. Put your file(s) there.

  2. Load your file:

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


The simplest and most effective way to make a file available to your component is this:

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

Note the json! before the path


I was trying to do the same thing and this is what worked for me (ES6/ES2015):

import myData from './data.json';

I got the solution from this answer on a react-native thread asking the same thing:

Related Query

More Query from same tag