1

I am learning ReactJS for a web application that uses JQuery to load JSON-encoded data from a remote source.

In this sample, this.state is manually set to JSON-encoded data

import React, { Component } from 'react';
import Projects from './Components/Projects';
import './App.css';

class App extends Component {
  constructor(){
    super();
    this.state = {
      projects: [
        {
          title: 'Buisness Website',
          category: 'Web Design'
        },
        {
          title: 'Social App',
          category: 'Mobile Development'
        },
        {
          title: 'Ecommerce Shopping Cart',
          category: 'Web Development'
        }
      ]
    }
  }

  render() {
    return (
      <div className="App">
        My App
        <Projects projects={this.state.projects}/>
      </div>
    );
  }
}

export default App;

I would like to use Jquery to load the data from a remote location. I tried

this.state =  $.getJSON('remote.json', function (data) {
        console.log(data);
});

Unfortunately, when running the site, I get an error: '$' is not defined. How can I fix this? Please note that I included

<script src="http://code.jquery.com/jquery-1.9.1.min.js"></script>

in the head of index.html

EDIT: Keep in mind that the JSON file will not be local.

3
  • you forgot some jquery versions :)) why do you have 2 jquery files? Commented May 31, 2017 at 6:28
  • 2
    you need to import $ from jquery, like this: import $ from 'jquery'; check this answer, it will help you: stackoverflow.com/a/44259149/5185595 Commented May 31, 2017 at 6:31
  • @madalin_ivascu editied :) Commented May 31, 2017 at 6:34

2 Answers 2

2

From my understanding, you want to use jquery on your project. To fix your issue, you just have to install jquery and include it in your project, like so in your command line:

npm install --save jquery

then on the file that uses jquery, import it:

import $ from 'jquery';

It looks like you are trying to set a state based on the result of the getJSON method, a correct way of doing it would be something like this:

$.getJSON('remote.json',(data) => {
  this.setState({ myStateData: data })
});

An alternative, would be to use the fetch, and your code would look something like this:

fetch('remote.json').then(data => this.setState({ myStateData: data }));

The above examples assume your state has the property myStateData.

Sign up to request clarification or add additional context in comments.

1 Comment

Works perfectly now.
2

Instead of using Jquery to load a localJSON file you can configure your webpack

First install json-loader with

npm i json-loader --save-dev

Then configure your webpack with this loader

loaders: [
  { test: /\.json$/, loader: 'json' },
  // other loaders 
]

Now in your file you can import your json like

   import remote from '.path/to/remote.json';

However if your json files are remote, you have multiple options to do ajax requests

You can use packages like axios, node-fetch, jquery

Since you are using jquery , you should install

   npm install -S jquery

and import as

  import $ from 'jquery'

2 Comments

Will this work if the JSON file is not local? I have been keeping it local for testing purposes, but eventually I will want to retrieve it from a server. Sorry for the confusion.
No it wont work, for remote files. In that case its better to have ajax request with npm packages node-fetch or axios.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.