InfoHeap
Tech
Navigation
  • Home
  • Tutorials
    • CSS tutorials & examples
    • CSS properties
    • Javascript cookbook
    • Linux/Unix Command Line
    • Mac
    • PHP
      • PHP functions online
      • PHP regex
    • WordPress
  • Online Tools
    • Text utilities
    • Online Lint Tools
search

React Tutorials

  • Overview
  • Hello world (jsx)
  • jsx to javascript (babel cli)
  • jsx offline transformation
  • Hello world (javascript)
  • click hander (jsx)
  • click handler (javascript)
  • clock example
  • ajax
  • React FAQ
 
  • Home
  • > Tutorials
  • > Javascript
  • > React

React component ajax example

on Feb 17, 2016

React ajax calls can be done in componentDidMount. Once we get the result, we can use setState to re-render the component. In case component can potentially unmount, we should clean any pending ajax calls in componentWillUnmount. You may also want to read React – Component Specs and Lifecycle.

Here is the code snippet:

<script src="https://fb.me/react-0.14.7.js"></script>
<script src="https://fb.me/react-dom-0.14.7.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/babel-core/5.8.23/browser.min.js"></script>
<script src="//ajax.googleapis.com/ajax/libs/jquery/2.1.4/jquery.min.js"></script>

<div id="mycomp"></div>
<script type="text/babel">
var MyComp = React.createClass({
  getInitialState: function(){
    return {msg:  "-"};
  },
  componentDidMount: function() {
    var obj = this;
    this.reqHandle = $.getJSON("/api/get-json-hello-world.php", function (data) {
      obj.setState({msg: data["msg"]});
    });
  },
  render: function() {
    return (<div>{this.state.msg}</div>);
  },
  componentWillUnmount: function() {
    this.reqHandle.abort();
  }
});
ReactDOM.render(<MyComp/>, document.getElementById('mycomp') );
</script>
refresh done
try it online

Few points to note

  1. The url /api/get-json-hello-world.php returns the following json object:

    {"msg":"Hello World"}
    
  2. jQuery getJSON automatically parsed the returned json.
  3. jQuery getJSON call returns a handle which can be used to abort the request later if needed.

Suggested posts:

  1. Requirejs – quickstart guide for beginners
  2. React jsx hello world with offline transformation
  3. CSS – align text in center horizontally
  4. ReactJS – convert jsx to javascript using babel cli
  5. React.render vs ReactDOM.render – which one should be used?
  6. PHP array map example
  7. Python xrange examples
  8. CSS general sibling selector
Share this article: share on facebook share on linkedin tweet this submit to reddit
Posted in Tutorials | Tagged Javascript, jQuery, React, Tutorials, Web Development
  • Browse content
  • Article Topics
  • Article archives
  • Contact Us
Popular Topics: Android Development | AngularJS | Apache | AWS and EC2 | Bash shell scripting | Chrome developer tools | Company results | CSS | CSS cookbook | CSS properties | CSS Pseudo Classes | CSS selectors | CSS3 | CSS3 flexbox | Devops | Git | HTML | HTML5 | Java | Javascript | Javascript cookbook | Javascript DOM | jQuery | Kubernetes | Linux | Linux/Unix Command Line | Mac | Mac Command Line | Mysql | Networking | Node.js | Online Tools | PHP | PHP cookbook | PHP Regex | Python | Python array | Python cookbook | SEO | Site Performance | SSH | Ubuntu Linux | Web Development | Webmaster | Wordpress | Wordpress customization | Wordpress How To | Wordpress Mysql Queries | InfoHeap Money

Copyright © 2025 InfoHeap.

Powered by WordPress