2

I have 2 paths:

  • /blog
  • /blog/:slug

The first path's content shows a list of links to the second path. When clicking on a link, the new page is displayed using a ReactCSSTransitionGroup from my App.js file:

...
const Routes = React.cloneElement(this.props.children, {
      data: data,
      key: path
    });

    return (
      <div id="js-body" className={data.titleColor}>
        <Nav data={ data }/>
        <ReactCSSTransitionGroup
          transitionName={transitionName}
          transitionAppear={true}
          transitionLeave={true}
          transitionAppearTimeout={timeout * 2}
          transitionEnterTimeout={timeout * 2}
          transitionLeaveTimeout={timeout}
          component="div"
          className="animation-container">

          <h2 className="title"><span>{data.pageTitle}</span></h2>
          { Routes }

        </ReactCSSTransitionGroup>
        <Footer data={ data }/>
      </div>
    );

Now on my second path (/blog/:slug) I check the slug and make a call to my store to fetch the data for that post. If no data exists I show a 404 page else I show the posts data.

...

export default class BlogPost extends Component {

  componentWillMount() {
    this._getPageData();
  }

  componentDidMount() {
    const data = this.props.data;
    const pageTitle = (data.page) ? data.page.title : 'Page Not Found';
    document.title = config.site.title + ' | ' + pageTitle;

    // Enable code highlighting
    $('pre code').each(function(i, block) {
      hljs.highlightBlock(block);
    });
  }

  _getPageData(){
    AppDispatcher.dispatch({
      action: 'get-page-data',
      page_slug: 'blog',
      post_slug: this.props.params.slug
    });
  }

  render() {
    ...

    if (!post.fields) {
      return(
        <NoMatch />
      );
    }

    ...


    return (...);
  }
}

The problem comes at this stage. When I click the Link element to go back to /blog from a valid /blog/:slug page, before the transition happens, my current post changes to my 404 page and then transitions out to display the /blog page content. I did a couple of tests and these were my findings:

  • When clicking the Link element, the BlogPost component never hits the componentWillUnmount lifecycle function
  • When clicking the Link element, the BlogPost component tries to re-render itself. However at this point the /blog data has already been requesting, not finding post.fields, hence displaying the 404 page prior to transitioning out.
  • When adding an extra condition for the 404 not to show up (say, only show 404 if we know that we don't have a post for sure), I get the following errors:

first:

Warning: There is an internal error in the React performance measurement code. Did not expect componentDidMount timer to start while render timer is still in progress for another instance.

followed by:

Uncaught TypeError: Cannot read property 'createdAt' of undefined

Again, these happening because it's attempting to re-render it before transitioning out, but the data of the page being requested has already been fetched, hence being unable to find the 'createdAt' property.

Any idea on why my component is trying to re-render itself on request for a new component?

Marcos Mellado
  • 125
  • 2
  • 11
  • 1
    Did you manage to find a solution? Am stuck with the same error... – Coder Absolute Sep 08 '16 at 13:45
  • 1
    I did in what may be somehow hacky. Basically what was happening was that by clicking the link to show the new view, I'm overriding the data object in which the data for the current view is displayed with the data of the view to be displayed, hence, showing the new content before the transition happens. My solution was to simply use a different data object for each view. That way when transitioning from one view to another, the state object changes, but the data object for the existing view doesn't, and the transition happens successfully. – Marcos Mellado Sep 09 '16 at 18:22
  • 1
    TLDR: Use a different data object for each view and let the transition trigger using the your React state – Marcos Mellado Sep 09 '16 at 18:25
  • 1
    Thanks for letting me know! – Coder Absolute Sep 11 '16 at 10:24

0 Answers0