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.
$fromjquery, like this:import $ from 'jquery';check this answer, it will help you: stackoverflow.com/a/44259149/5185595