loading json data from local file into React JS


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 as a global

'use strict';

var React = require('react/addons');

// load in JSON data from file
var data;

var oReq = new XMLHttpRequest();
oReq.onload = reqListener;
oReq.open("get", "data.json", true);

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

var List = React.createClass({
  getInitialState: function() {
    return {data: this.props.data};    
  render: function() {
    var listItems = this.state.data.map(function(item) {
        var eachItem = item.works.work;        

        var photo = eachItem.map(function(url) {
            return (
    return <ul>{listItems}</ul>

var redBubble = React.createClass({
    render: function() {
      return (
          <List data={data}/>          

module.exports = redBubble;

Ideally, I would prefer to do it something like this, but it's not working - it tries to add ".js" onto the end of the filename.

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

Any advice on the best way, preferably the "React" way, would be much appreciated!

This question is tagged with javascript ajax json xmlhttprequest reactjs

~ Asked on 2015-08-01 04:03:21

The Best Answer is


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: https://stackoverflow.com/a/37781882/176002

~ Answered on 2016-09-05 15:30:07


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

~ Answered on 2015-10-15 06:39:43

Most Viewed Questions: