jeudi 30 juin 2016

How to pass ajax data to React component?

I am new to react js.I am just getting a data via ajax and manipulate the data in component.But always I am getting undefined.I Know that my ajax call is working but I don;t know How to Handle that data in component.

App.js

    var React = require('react');
var Actions = require('../actions');
var Store = require('../stores/store');
var Nav =require('./Nav');
var Fakeprofile = require('./Fakeprofile');
var Sidemenu = require('./Sidemenu');
var Bulkmail = require('./Bulkmail');
var store = require('../stores/store');
var api = require('../utils');
function getAppState() {

    return {
        //result:store.setProfile()
        result:api.getprofile()
    }
}


var App = React.createClass({
   getInitialState:function () {
       return getAppState();
   },
    componentDidMount: function(){
        Store.addChangeListener(this._onChange);
    },

    componentUnmount: function(){
        Store.removeChangeListener(this._onChange);
    },



    render:function () {
        console.log(this.state);
        return(
            <div>
                <Nav/>
                <Sidemenu/>
                <Fakeprofile />



            </div>

        )
    },
    _onChange: function(){
        this.setState(getAppState());
    }
});

module.exports = App;

utils.js

var actions = require('./actions');

module.exports = {
  getprofile:function () {
      console.log('Gettinf data');
      var url  = 'http://localhost:3000/api/index';
      $.ajax({
          url:url,
          dataType:'json',
          cache:false,
          success:function success(data) {
              console.log(data);



          },
          error:function error(xhr,status,err) {
              console.log(err);
          }
      })
  }
};

Aucun commentaire:

Enregistrer un commentaire