1

My App component in React.js currently accesses hard-coded data stored in a js variable. I want to instead fetch this piece of data by making a GET or POST request to the python flask server running for the application.

React.renderComponent( <App data={dataset}/>, document.getElementById( "App" ) );

Let's say the data can be accessed by the url /get_dataset.json on python flask. How do alter the code here?

1
  • 2
    There is nothing specific that React adds for this use case. So you could use whatever tools you like (e.g. jQuery) to make an ajax request, retrieve the data from your endpoint and then call React.renderComponent() w/ the data you have now available. Commented Apr 20, 2015 at 21:04

1 Answer 1

3

Assuming you're using jQuery for ajax, you could wait to render the component until after you get the response:

$.get('/get_dataset.json', function(dataset) {
  React.renderComponent( <App data={dataset}/>, document.getElementById( "App" ) );
});

or you could do it inside the component as seen here

Sign up to request clarification or add additional context in comments.

1 Comment

Logically, this should work. But the React.renderComponent isn't been called from with the $.get() ajax call. I tried passing my good old static data to React.renderComponent from within the $.get() call. Even that did not work.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.