Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Component doesn't rerender on props change #90

Open
alexandr2110pro opened this issue Dec 16, 2017 · 5 comments
Open

Component doesn't rerender on props change #90

alexandr2110pro opened this issue Dec 16, 2017 · 5 comments

Comments

@alexandr2110pro
Copy link

Hi. Sorry if it is a duplicate but here is the issue:

AppRoot Component:

import React, { Component } from 'react';
import PropTypes from 'prop-types';
import { connect } from 'react-redux';

import {
  getModelData,
  isContentsConfigLoaded,
} from '../lib/services/reducers';

import {
  requestEntities,
  loadContentsConfig,
  preloadLargeContentData,
} from '../lib/services/actions/actions';

import { DialogModel, TranslationsTableModel } from '../lib/models';
import { DialogsStack } from './DialogsStack';
import { Location } from '../components/Location/Location';

import './webflow-overrides.css';

const mapStateToProps = (state, props) => ({
  dialogsStack: getModelData(state, DialogModel, 'all'),
  configReady: isContentsConfigLoaded(state),
});

const mapDispatchToProps = {
  requestEntities,
  loadContentsConfig,
  preloadLargeContentData,
};


@connect(mapStateToProps, mapDispatchToProps)
export class AppRoot extends Component {

  static propTypes = {
    requestEntities: PropTypes.func.isRequired,
    configReady: PropTypes.bool.isRequired,
    dialogsStack: PropTypes.arrayOf(PropTypes.object),
    loadContentsConfig: PropTypes.func.isRequired,
    preloadLargeContentData: PropTypes.func.isRequired,
  };

  componentWillMount() {
    this.props.requestEntities(DialogModel, 'all');
    this.props.requestEntities(TranslationsTableModel, 'all');
    this.props.loadContentsConfig();
  }

  componentWillReceiveProps(nextProps, nextContext) {
    console.warn('AppRoot componentWillReceiveProps');
    if (nextProps.configReady && !this.state.preloadingStarted) this._startPreloadingData();
  }

  state = { preloadingStarted: false }

  render() {
    const { dialogsStack } = this.props;
    console.warn('render!', dialogsStack);

    return (
      <div className="app-root">
        <Location />
        {
          dialogsStack && dialogsStack.length
            ? <DialogsStack dialogsStack={dialogsStack} />
            : <div>Loading</div>
        }
      </div>
    );
  }

  _startPreloadingData() {
    const { preloadLargeContentData } = this.props;
    this.setState({ preloadingStarted: true });
    preloadLargeContentData();
  }
}

here is the console from the browser:
screenshot 2017-12-16 22 29 50

(I have a bunch of other debugging console.warn in other parts as you can see. They are just displaying the request was successful so that the Redux store would update as expected.)


But when I run the react-snapshot, here is what I'm getting:

screenshot 2017-12-16 22 36 38

As you see, the render is called only once, and the componentWillReceiveProps has not been called at all.

So it always renders the following as the snapshot:

    <div class="app-root">
      <div>Loading</div>
    </div>
@alexandr2110pro alexandr2110pro changed the title Components doesn't rerender on props change Component doesn't rerender on props change Dec 16, 2017
@stereobooster
Copy link

This happens because current version can't "wait" for requests to finish. It supposed to be fixed in API v2 #30

@alexandr2110pro
Copy link
Author

I suppose the same happens with the google crawler bot.

@alexandr2110pro
Copy link
Author

@stereobooster do you have any information on when it will be merged?

@stereobooster
Copy link

It is not finished. No information on when it will be merged

@stereobooster
Copy link

API v2 alone can be not sufficient for your case. Assume pre-renderer waited long enough, to render all HTML. On the client side browser receives all HTML, React application starts to execute, its state is empty, React erases prerendered HTML and places "Loading" starts to do AJAX request to get data. What you can do is:

If you would use async components, you would need to store a state of loaded components too. For example like in loadable-components

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants