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.

Weird renders on connected Components

See original GitHub issue

There is a strange bug that appears when i switch from a normal <Router> to a <ConnectedRouter>.

Whenever a component is connected, the ConnectedRouter (as mentioned in other issues) re-renders on every store update, but another weird issue occurs that i really can’t pin it down to something particular.

What happens is that, when a random action is fired, any component that is connect-ed renders again, without the wrapping connect HOC actually rendering. What i mean is that in this scenario:

const MyComponent = () => <h1>hi</h1>;

export default connect()(MyComponent);

the Connect(MyComponent) (the HOC returned from connect) doesn’t render, but the MyComponent does. Thus what i end up with, is that on every redux action that changes the global state, every component that is wrapped with connect() will render again regardless of the props it has or has subscribed to.

You can find a a codesandbox version of what I’m describing (using your example configuration) here:

— > https://codesandbox.io/s/yv7yqkx6nz

As you can see, every time MyComponent renders it prints the word “rendered”. If i dispatch a dummy action window.store.dispatch({ type: "" }); then MyComponent renders (although it shouldn’t). If I keep everything as is, but simply replace ConnectedRouter with Router, then MyComponent doesn’t re-render (which is correct since it’s not dependant on any state from redux).

I’m not mistaken this is a performance bug for any app that uses this package.

Let me know 😃

Issue Analytics

  • State:open
  • Created 5 years ago
  • Comments:7 (1 by maintainers)

github_iconTop GitHub Comments

3reactions
3nvicommented, Jan 21, 2019

@afitiskin Hey there,

the thing is that under the hood connect already implements a PureComponent logic (that’s why whenever your redux props stay the same your component doesn’t re-render).

ConnectedReactRouter simply bypasses that while a normal Router doesn’t. Although this will be a solution, I wouldn’t want to force an explicit PureComponent on all my connected components, since they already have that through the connect() HOC 😕

0reactions
Elyx0commented, Jun 5, 2020

Okkay Strict Mode…

Strict mode can’t automatically detect side effects for you, but it can help you spot them by making them a little more deterministic. This is done by intentionally double-invoking the following functions:

Class component constructor, render, and shouldComponentUpdate methods
Read more comments on GitHub >

github_iconTop Results From Across the Web

Strange problem using conditional rendering of component
I have a simple React component that takes in a single prop, if this prop is 0 show one component otherwise show another....
Read more >
Weird/Incorrect results from SimpleITK:ConnectedComponent()
Hi there, I've come across some unexpected behavior while using the procedural interface of the ConnectedComponentImageFilter in SimpleITK.
Read more >
This vertex is giving me weird artifacts when I render. How can ...
You rearrange the topology by using tools like the Knife tool to create some extra edges to connect to and then dissolving edges...
Read more >
Hooks FAQ - React
Do Hooks replace render props and higher-order components? What do Hooks mean for popular APIs like Redux connect() and React Router?
Read more >
Weird flickering from video source as texture
My rendered animation has a weird flickering in it specifically from a video source used as texture and emission.
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