preact-portal icon indicating copy to clipboard operation
preact-portal copied to clipboard

`renderLayer` called on _every_ update?

Open lydell opened this issue 6 years ago • 4 comments

The point of <Portal> is to pass children to it (that are rendered somewhere else). And props.children !== prevProps.children always, right? If so, doesn't this mean that the for-if check in componentDidUpdate always triggers?

https://github.com/developit/preact-portal/blob/ba212795be780de0d0a0c9725c69b1998e6f1123/src/preact-portal.js#L10-L16

lydell avatar Mar 03 '19 18:03 lydell

This is true of any Preact/react component. Update Cascade can be mitigated using shouldComponentUpdate or memo.

Fwiw the render() call diffs, so what's happening here isn't necessarily a DOM mutation, but rather the flow of data through the virtual DOM tree.

developit avatar Mar 11 '19 14:03 developit

Ok! But the for-if loop does not do anything here, right? So it could be removed?

 componentDidUpdate(props) { 
 	return setTimeout(this.renderLayer); 
} 

lydell avatar Mar 11 '19 15:03 lydell

Technically it's possible to avoid triggering rerenders by hoisting things out of render:

const myPortal = <Portal into="body"><SomeComponent /></Portal>

class Outer {
  render() {
    return <div>{myPortal}</div>
  }
}

developit avatar Mar 12 '19 17:03 developit

Thanks for the suggestion! I'm not having a performance problem or anything though, I'm just trying to understand how these things work:

Here's a codesandbox showing what I mean: https://codesandbox.io/s/k54oz8mzm3?fontsize=14

I the demo, the <Portally> component always says that props.children change (even though they don't really, kind of).

lydell avatar Mar 12 '19 20:03 lydell