[question] should shouldUpdateComponent hook be considered a hint?
See original GitHub issueHi, 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:
- Created 6 years ago
- Comments:46 (15 by maintainers)
Top GitHub Comments
Ah yep - that’s a super important distinction. Appreciate you raising the issue on both sides @rosenfeld 🙌
Oh also you mentioned a discussion group - we have an active Slack chat with about 360 people in it! https://preact-slack.now.sh