question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

unimplement shouldComponentUpdate?

See original GitHub issue

There was some good discussion on Twitter (between @gaearon, @markerikson, @timdorr, @mjackson, @ryanflorence, et al.) about connect’s component implementing shouldComponentUpdate.

If I understand correctly, the main issue being that any component wrapped in connect then blocks updates related to context changes, and this has an adverse impact on libraries that pass data via context that need to trigger rerenders (such as React Router’s Link component.)

The changes in the next branch of React Redux make its implementation of shouldComponentUpdate much less necessary, mainly because now setState is no longer called as a response to every store state change, but only if the final merged props have changed. So now when shouldComponentUpdate is called as a result of calling setState, it’s always going to return true anyways. (The call to setState could probably be replaced with forceUpdate and would work exactly the same.)

In the case of shouldComponentUpdate being called after receiving new props from parent, it’s effectively just acting like PureComponent. That responsibility can be given to the components being wrapped, which would have better knowledge about if/how they should implement shouldComponentUpdate. I personally would use recompose and do something like:

export default compose(
   pure, // <-- from recompose
   connect(...),
)(MyComponent);

An alternative to removing connect’s shouldComponentUpdate completely would be to make it an another option argument, and decide whether it should be opt-in or opt-out.

Issue Analytics

  • State:closed
  • Created 7 years ago
  • Reactions:25
  • Comments:23 (18 by maintainers)

github_iconTop GitHub Comments

14reactions
timdorrcommented, Sep 28, 2016

Just to be clear and make sure everyone understands what’s going on, here is the situation with something like react-redux and React Router:

<Router>
  <connect(App)>
    <Link to="/foo" activeClassName="active">

In this case, <Router> is providing a context that <Link> is consuming something like this:

let className
if (this.context.location == this.props.to) 
  className = this.props.activeClassName

(That’s a crazy over-simplification, but it gets the point across)

If the URL/location on the page changes, then that information should propagate from the <Router> to the <Link> via this.context, causing it to potentially re-render. Context is a way to cross the boundaries of direct Component-to-Component communication via props.

The problem lies with shouldComponentUpdate not being able to take into account changes on this.context. The connect()ed component thinks nothing has changed with its props during this time (which is true!), so it tells React that it shouldn’t re-render this component (or anything underneath it, including the <Link>!).

Hopefully that explains the situation well enough. How to fix it is another matter 😃

11reactions
markeriksoncommented, Sep 28, 2016

Oh boy, this oughta be good… 😃

Read more comments on GitHub >

github_iconTop Results From Across the Web

Hooks FAQ - React
How do I implement shouldComponentUpdate? How to memoize calculations? How to create expensive objects lazily? Are Hooks slow because of creating functions ...
Read more >
Using shouldComponentUpdate() · react-indepth
This method allows your Component to exit the Update life cycle if there is no reason to apply a new render. Out of...
Read more >
fulcro.client.impl.protocols — fulcrologic/fulcro 2.8.13 - cljdoc
Get the ident for the given component (UNIMPLEMENTED AT PRESENT) ... shouldComponentUpdate. (shouldComponentUpdate this next-props next-state).
Read more >
React-lifecycle-hooks NPM | npm.io
... Do nothing if the component's `shouldComponentUpdate` returns a boolean, ... fill - auto fill unimplemented lifecycle with function() {} .
Read more >
Implement React Redux from Scratch (Part 3) | by Kaijie Huang
It has a flag shouldComponentUpdate which will be used in the actual react ... The temporal method will unimplemented itself when called and ......
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found