Skip to content

Commit

Permalink
Context consumer unmounting perf (#4526)
Browse files Browse the repository at this point in the history
  • Loading branch information
JoviDeCroock authored Oct 6, 2024
1 parent 87f7efa commit ad3bc3b
Showing 1 changed file with 5 additions and 5 deletions.
10 changes: 5 additions & 5 deletions src/create-context.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,8 @@ export function createContext(defaultValue, contextId) {
/** @type {FunctionComponent} */
Provider(props) {
if (!this.getChildContext) {
/** @type {Component[] | null} */
let subs = [];
/** @type {Set<Component> | null} */
let subs = new Set();
let ctx = {};
ctx[contextId] = this;

Expand All @@ -31,19 +31,19 @@ export function createContext(defaultValue, contextId) {

this.shouldComponentUpdate = function (_props) {
if (this.props.value !== _props.value) {
subs.some(c => {
subs.forEach(c => {
c._force = true;
enqueueRender(c);
});
}
};

this.sub = c => {
subs.push(c);
subs.add(c);
let old = c.componentWillUnmount;
c.componentWillUnmount = () => {
if (subs) {
subs.splice(subs.indexOf(c), 1);
subs.delete(c);
}
if (old) old.call(c);
};
Expand Down

0 comments on commit ad3bc3b

Please sign in to comment.