1

I was trying to display json data in a table format based on this answer. Everytime I run this, it shows this error Line 15: '$' is not defined no-undef. I am starting the ajax call in line 15. I've tried adding const $ = window.$; on the top of the code, but that doesn't solve anything.

class App extends React.Component {
    constructor(){
      super() 
        this.state = {
          data: []
        }
      
    }
    componentDidMount() {
      $.ajax({
         url: "http://www.mocky.io/v2/5c3f2a6c3500004d00ec3789",
         type: "GET",
         dataType: 'json',
         ContentType: 'application/json',
         success: function(data) {
           
           this.setState({data: data});
         }.bind(this),
         error: function(jqXHR) {
           console.log(jqXHR);
         }.bind(this)
      })
    }
    render() {
      
          
      return (
        <table>
        <tbody>{this.state.data.map(function(item, key) {
               
                 return (
                    <tr key = {key}>
                        <td>{item.name}</td>
                        <td>{item.post}</td>
                        <td>{item.country}</td>
                        <td>{item.contact}</td>
                    </tr>
                  )
               
               })}</tbody>
         </table>
      )
    }
  }
  


ReactDOM.render(<App />, document.getElementById('root'));

Any help with the error?

2
  • It looks like not able to find jQuery library. Please import jQuery library before your script code loaded in DOM. Commented Jan 17, 2019 at 11:51
  • Did you imported jQuery ? import jQuery from 'jquery' Commented Jan 17, 2019 at 11:52

3 Answers 3

11

When using create-react-app, First You need to install the Jquery package like this.

For Mac

sudo npm install jquery --save 

For Windows

npm install jquery --save

Now in the JS files you can use jquery

import $ from 'jquery';
Sign up to request clarification or add additional context in comments.

4 Comments

Is sudo needed to install a non-global node dependency?
that resolved the error. But my table isn't showing up in the browser
sudo is used in Mac environment, when you use Windows try without sudo (npm install jquery --save )
I had to use import * as $ from 'jquery';. The import statement in the answer resulted in an error for me.
1

You need to include jQuery library.

import $ from 'jquery'; 

make sure jquery package is installed.

6 Comments

that resolved the error. But my table isn't showing up in the browser
use console.log to see whether the state is updated properly
It doesn't hit the ajax success function.
used datatype jsonp instead of json. That solved the problem
put a console.log(data); inside your success function to see if your data arrives as it should be. if it is okay then it has something to do with your state update
|
1

You might not even need jQuery in React, the fetch API exposes by the browser just works fine. Rather than installing the entire jQuery library to just handle Ajax request, I would go instead with axios which in my opinion is lightweight than jQuery.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.