jquery - Consuming a RESTful service in Reactjs -
im starting reactjs , want make request restful service, here code, can me please?
render: function() { $.ajax({ url: "http://rest-service.guides.spring.io/greeting" }).then(function(data) { $('.greeting-id').append(data.id); $('.greeting-content').append(data.content); return <div style="background-color: #ffffff"> <p class="greeting-id">the id </p> <p class="greeting-content">the content </p> </div>; } }
i load jquery in html react, still cant make work, i'm missing important? or there better way this? dont want use thing, react , jquery, or more necesary?
thank help.
make request in componentdidmount method , use setstate method assign data returned restful service component's state. in render method can access through this.state.greetingid , this.state.greetingcontent properties:
componentdidmount: function(){ $.ajax({ url: "http://rest-service.guides.spring.io/greeting" }).then(function(data) { this.setstate({ greetingid: data.id, greetingcontent: data.content }); } )}, render: function() { return <div style="background-color: #ffffff"> <p class="greeting-id">the id {this.state.greetingid}</p> <p class="greeting-content">the content {this.state.greetingcontent}</p> </div>; }
refer following link details: https://facebook.github.io/react/tips/initial-ajax.html
Comments
Post a Comment