Jquery in React is not defined

后端 未结 5 1824
梦如初夏
梦如初夏 2020-11-27 04:35

Hi I just want to receive ajax request, but the problem is that jquery is not defined in React. React version is 14.0

Error message

 U         


        
相关标签:
5条回答
  • 2020-11-27 05:16

    Isn't easier than doing like :

    1- Install jquery in your project:

    yarn add jquery
    

    2- Import jquery and start playing with DOM:

    import $ from 'jquery';
    
    0 讨论(0)
  • 2020-11-27 05:23

    Just a note: if you use arrow functions you don't need the const that = this part. It might look like this:

    fetch('http://jsonplaceholder.typicode.com/posts') 
      .then((response) => { return response.json(); }) 
      .then((myJson) => { 
         this.setState({data: myJson}); // for example
    });
    
    0 讨论(0)
  • 2020-11-27 05:24

    Try add jQuery to your project, like

    npm i jquery --save
    

    or if you use bower

    bower i jquery --save
    

    then

    import $ from 'jquery'; 
    
    0 讨论(0)
  • 2020-11-27 05:30

    I just want to receive ajax request, but the problem is that jQuery is not defined in React.

    Then don't use it. Use Fetch and have a look at Fetch polyfill in React not completely working in IE 11 to see example of alternative ways to get it running

    Something like this

    const that = this; 
    fetch('http://jsonplaceholder.typicode.com/posts') 
      .then(function(response) { return response.json(); }) 
      .then(function(myJson) { 
         that.setState({data: myJson}); // for example
      });
    
    0 讨论(0)
  • 2020-11-27 05:42

    Add "ref" to h1 tag :

    <h1 ref="source">Hey there.</h1>
    

    and
    const { source } = this.props; change to const { source } = this.refs;

    0 讨论(0)
提交回复
热议问题