Render children at most once through the use of Singletons
Proposed Feature
React.createSingleton
Returns a unique React component which could be used anywhere but its children would only get rendered a single time for every location the returned Singleton is used. Very useful for declaring SVG filters but can be combined with anything else including Portals, Fragments and so on.
Use Case
We have a Ripple component similar to Material-UI which could get applied to anything including a div
and a button
. Our Ripple effect makes use of an SVG filter for a gooey effect. It's easy for us to bake the SVG into the Ripple component but we only need the SVG rendered once, anywhere in the DOM.
We make use of the Singleton component to handle rendering this SVG once and when the component rendering the SVG gets destroyed, the responsibility falls upon the next component in the list, if any.
Example
const FilterSingleton = React.createSingleton();
const FancyEffect = () =>
{
return (
<div>
<FilterSingleton>
<svg className='svg-defs' xmlns='http://www.w3.org/2000/svg'>
<defs><filter id='can-be-used-anywhere'></filter></defs>
</svg>
</FilterSingleton>
<SomeComponent ... />
<SomeComponent ... />
<SomeComponent ... />
</div>
);
};
// Used all throughout your application but SVG is only rendered at most once on the DOM
<FancyEffect />
<FancyEffect />
<FancyEffect />
Implementation
React.createSingleton = function()
{
const instances = [ ];
return class Singleton extends PureComponent
{
constructor (props) {
super (props);
instances.push (this);
}
componentWillUnmount() {
const index = instances.indexOf (this);
if (index > -1) {
instances.splice (index, 1);
}
if (instances[0]) {
instances[0].forceUpdate();
}
}
render() {
return instances[0] === this ? this.props.children : null;
}
};
};
推荐相似问题
Warning: Received `true` for a non-boolean attribute `someProp`
ComponentDidCatch is not working properly React js16.2.0
When React JS version 15.x is going to be deprecated ? Can any one please enlight the steps to migration from 15.x to 16.x version ?
[create-react-class] missing a interface that as constructor in es6 class
forwardRef causes componentWillReceiveProps after setState
Use and set a component's state within setInterval function
Child component correctly deletes parent's state's object entry, but wrong child is unmounted upon re-render
Why are the Consumer and Provider properties of Consumer?
Context API bitmask related questions
Hovering on specific element affects other elements
基本信息
- 回复数:329
- 讨论框架:react
- 原始内容:查看信息
- 最后更新于:2020-10-23
相关讨论
- 1. Reconciliation has bug that is occured when client starts hydration
- 2. Bug: React function component unmounts on state change.
- 3. Bug: SuspenseList revealOrder="backwards" is not consistent without tail props
- 4. Bug: disableRemotePlayback not recognized
- 5. Bug: react test utils do not work together with esm module
1条回答
Hey @dkrutsko. If you'd like to propose a new API for React you should do so via the RFC process, which you can find here: https://github.com/reactjs/rfcs
Thanks!