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.

[question] should shouldUpdateComponent hook be considered a hint?

See original GitHub issue

Hi, I’d like to apologize myself upfront if this is not the proper place to ask questions, but I couldn’t find any discussion group or so. Please let me know where I should ask such questions.

This is what React says about shouldUpdateComponent:

https://facebook.github.io/react/docs/react-component.html#shouldcomponentupdate

Currently, if shouldComponentUpdate() returns false, then componentWillUpdate(), render(), and componentDidUpdate() will not be invoked. Note that in the future React may treat shouldComponentUpdate() as a hint rather than a strict directive, and returning false may still result in a re-rendering of the component.

I’d like to know if the same is valid for Preact as well. If this behavior changes it would possibly affect components that rely on that current behavior, so I feel quite scared about the possibility that this behavior could change in the future. I’d like to know what Preact and Inferno think about this so that I could feel confident about relying on that behavior and just drop React support once they decide to ignore the false return value.

I’ve been thinking a lot about an autocomplete solution for our application that supports multiple selections, so let me explain why I think this is relevant. One of the approaches I’ve been considering is to use an structure such as:

render() {
  return <div className={uniqueComponentClassName}>
    <InputComponent className={uniqueInputClassName} />
    <SelectionDisplay selected={selectedItems} />
    <OptionsMenu onShouldUpdateComponent={_=> false} className={uniqueOptionsMenuClassName} />
  </div>
}

In componentWillMount I would register some global event handlers that would compare event.target against some known unique css class names. Then I’d like to be able to populate the OptionsMenu with whatever I want knowing that that component would remain untouched by Preact. There are many reasons why this would be interesting. For example, I noticed with our current autocomplete custom component based on jQuery UI that when we have to render thousands of options, using cloneNode instead of createElement can be much faster, specially in some browsers. So, I could decide to use that to generate the menu items, for example. Then, as the items are selected, I’d like to update the state of the autocomplete component so that SelectionDisplay is updated correctly as well as other parts of the applications, but I don’t want Preact to touch the content of OptionsMenu. In that case, if we ignore the onShouldUpdateComponent results the component wouldn’t just become slower. It wouldn’t work correctly at all. That’s why I’m concerned if Preact would ever consider ignoring the return value from shouldComponentUpdate. It’s a valuable escape hatch, specially when integrating to third party components not managed by the vdom implementation. Does that make sense to you?

Issue Analytics

  • State:closed
  • Created 6 years ago
  • Comments:46 (15 by maintainers)

github_iconTop GitHub Comments

1reaction
developitcommented, Jun 22, 2017

Ah yep - that’s a super important distinction. Appreciate you raising the issue on both sides @rosenfeld 🙌

1reaction
developitcommented, Jun 12, 2017

Oh also you mentioned a discussion group - we have an active Slack chat with about 360 people in it! https://preact-slack.now.sh

Read more comments on GitHub >

github_iconTop Results From Across the Web

Hooks FAQ - React
Should I use Hooks, classes, or a mix of both? Do Hooks cover all use cases for classes? Do Hooks replace render props...
Read more >
React Hooks cheat sheet: Best practices with examples
Editor's note: This React Hooks tutorial was last updated in January 2021 to include more React Hooks best practices and examples.
Read more >
React Hooks - How do I implement shouldComponentUpdate?
In a comment above Gabriele Petrioli links to the React.memo documentation that explains how to implement shouldComponentUpdate.
Read more >
ReactJS shouldComponentUpdate() Method - GeeksforGeeks
The shouldComponentUpdate method allows us to exit the complex react update life cycle to avoid calling it again and again on every ...
Read more >
React Hooks - Understanding Component Re-renders - Medium
Functional components using hooks will go through a lifecycle but without explicit lifecycle methods unlike their class based counterparts.
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