How can I prevent the component from rendering before the data is loaded?


No

I'm waiting for the prop to come out of the store called GetDealersStore, and the way I'm getting the data is by doing:

  componentWillMount () { GetDealersActions.getDealers(); }

I have tested the app and componentWillMount()it runs before the initial render with this feature

let dealerInfo;
if (this.state.dealerData) {
  dealerInfo = this.state.dealerData.dealersData.map((dealer) => {
    return (<div>CONTENT</div>);
  })
} else {
  dealerInfo = <p>Loading . . .</p>
}

But for the first second you can see the above condition <p>Loading . . .</p>on the screen elseand then render the rest of the render return (<div>CONTENT</div>);where the ifcondition is in. So I guess that means the render method has been fired twice because it's been waiting for data from the database.

The data from the database is not available on the first render, so how can I get that data before the first initial render happens?

math

You can't do this with a single component. You should follow the container component pattern to separate data from rendering.

let DealersContainer = React.createClass({
  getInitialState() {
    return {dealersData: []};
  },
  componentWillMount() {
    GetDealersActions.getDealers();
  },
  render() {
    let {dealersData} = this.state;
    return (<div>
      {dealersData.map((dealer) => {
        let props = dealer;
        return (<Dealer ...props />); // pass in dealerData as PROPS here
      })}
    </div>);
  }
});

Then update your Dealercomponent to receive props and render the actual content.

Related


How can I prevent the component from re-rendering? (with MWE)

gbewothcc How can I prevent a component from re-rendering without changing its props ? Code sandbox link/minimal working example . Move the cursor over Canvasand you can see that the console logs a number of "Canvas re-rendered"messages. At the top level, I pa

How can I prevent the component from re-rendering? (with MWE)

gbewothcc How can I prevent a component from re-rendering without changing its props ? Code sandbox link/minimal working example . Move the cursor over Canvasand you can see that the console logs a number of "Canvas re-rendered"messages. At the top level, I pa

How can I prevent the menu from showing until it is fully loaded?

username Good evening, is there a way to prevent all elements from showing in a nested side menu after a click or refresh. What I need is to show the element only in the menu or submenu that I click? Website: link . thank you very much. jQuery: if (window.loca

How can I prevent the menu from showing until it is fully loaded?

username Good evening, is there a way to prevent all elements from showing in a nested side menu after a click or refresh. What I need is to show the element only in the menu or submenu that I click? Website: link . thank you very much. jQuery: if (window.loca

Get data from api before rendering component

Alex I send 2 api requests before rendering the page: const Profile = { template: '#profile', attributes: null, photos: [], data: function () { return {attributes: Profile.attributes, photos: Profile.photos}; }, beforeRouteEnter

Get data from api before rendering component

Alex I send 2 api requests before rendering the page: const Profile = { template: '#profile', attributes: null, photos: [], data: function () { return {attributes: Profile.attributes, photos: Profile.photos}; }, beforeRouteEnter

Get data from api before rendering component

Alex I send 2 api requests before rendering the page: const Profile = { template: '#profile', attributes: null, photos: [], data: function () { return {attributes: Profile.attributes, photos: Profile.photos}; }, beforeRouteEnter

Get data from api before rendering component

Alex I send 2 api requests before rendering the page: const Profile = { template: '#profile', attributes: null, photos: [], data: function () { return {attributes: Profile.attributes, photos: Profile.photos}; }, beforeRouteEnter

Get data from api before rendering component

Alex I send 2 api requests before rendering the page: const Profile = { template: '#profile', attributes: null, photos: [], data: function () { return {attributes: Profile.attributes, photos: Profile.photos}; }, beforeRouteEnter

How can I have a jQuery mobile spinner before the data is loaded?

Steven Matthews EDIT: I'm specifically looking for a solution using jQuery Mobile. jQuery Mobile has specific methods (as far as I know) that do exactly what I want. I don't want normal Javascript to have a spinning gif. This is not why I am posting this quest

How can I have a jQuery mobile spinner before the data is loaded?

Steven Matthews EDIT: I'm specifically looking for a solution using jQuery Mobile. jQuery Mobile has specific methods (as far as I know) that do exactly what I want. I don't want normal Javascript to have a spinning gif. This is not why I am posting this quest