Skip to content

Commit

Permalink
Fix createContext update being blocked by sCU
Browse files Browse the repository at this point in the history
  • Loading branch information
marvinhagemeister committed Feb 9, 2023
1 parent 9b83481 commit cea22e0
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 1 deletion.
6 changes: 5 additions & 1 deletion src/create-context.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ export function createContext(defaultValue, contextId) {
/** @type {import('./internal').FunctionComponent} */
Provider(props) {
if (!this.getChildContext) {
/** @type {import('./internal').Component[]} */
let subs = [];
let ctx = {};
ctx[contextId] = this;
Expand All @@ -40,7 +41,10 @@ export function createContext(defaultValue, contextId) {
// c.context[contextId] = _props.value;
// enqueueRender(c);
// });
subs.some(enqueueRender);
subs.some(c => {
c._force = true;
enqueueRender(c);
});
}
};

Expand Down
44 changes: 44 additions & 0 deletions test/browser/createContext.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -928,4 +928,48 @@ describe('createContext', () => {
rerender();
expect(scratch.innerHTML).to.equal('<p>hi</p>');
});

it('should not call sCU on context update', () => {
const Ctx = createContext('foo');

/** @type {(s: string) => void} */
let update;
class App extends Component {
constructor(props) {
super(props);
this.state = { foo: 'foo' };
update = v => this.setState({ foo: v });
}
render() {
return (
<Ctx.Provider value={this.state.foo}>
<Child />
</Ctx.Provider>
);
}
}

const spy = sinon.spy();

class Child extends Component {
static contextType = Ctx;

shouldComponentUpdate() {
spy();
return false;
}

render() {
return <p>{this.context}</p>;
}
}

render(<App />, scratch);
expect(scratch.textContent).to.equal('foo');

update('bar');
rerender();
expect(scratch.textContent).to.equal('bar');
expect(spy).not.to.be.called;
});
});

0 comments on commit cea22e0

Please sign in to comment.