Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add optional callback to forceUpdate method #592

Merged
merged 2 commits into from
Apr 2, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion src/component.js
Original file line number Diff line number Diff line change
Expand Up @@ -83,9 +83,11 @@ extend(Component.prototype, {


/** Immediately perform a synchronous re-render of the component.
* @param {function} callback A function to be called after component is re-rendered.
* @private
*/
forceUpdate() {
forceUpdate(callback) {
if (callback) (this._renderCallbacks = (this._renderCallbacks || [])).push(callback);
renderComponent(this, FORCE_RENDER);
},

Expand Down
2 changes: 1 addition & 1 deletion src/preact.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ declare namespace preact {
setState<K extends keyof StateType>(state:Pick<StateType, K>, callback?:() => void):void;
setState<K extends keyof StateType>(fn:(prevState:StateType, props:PropsType) => Pick<StateType, K>, callback?:() => void):void;

forceUpdate(): void;
forceUpdate(callback?:() => void): void;

abstract render(props?:PropsType & ComponentProps<this>, state?:StateType, context?:any):JSX.Element;
}
Expand Down
45 changes: 45 additions & 0 deletions test/browser/spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -124,4 +124,49 @@ describe('Component spec', () => {
expect(WithDefaultProps.prototype.getDefaultProps).to.be.calledOnce;
});
});

describe('forceUpdate', () => {
it('should force a rerender', () => {
let forceUpdate;
class ForceUpdateComponent extends Component {
componentWillUpdate() {}
componentDidMount() {
forceUpdate = () => this.forceUpdate();
}
render() {
return <div />;
}
}
sinon.spy(ForceUpdateComponent.prototype, 'componentWillUpdate');
sinon.spy(ForceUpdateComponent.prototype, 'forceUpdate');
render(<ForceUpdateComponent />, scratch);
expect(ForceUpdateComponent.prototype.componentWillUpdate).not.to.have.been.called;

forceUpdate();

expect(ForceUpdateComponent.prototype.componentWillUpdate).to.have.been.called;
expect(ForceUpdateComponent.prototype.forceUpdate).to.have.been.called;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for adding these tests, although it doesn't actually check that the component got re-rendered. Ideally the test would do something mirroring a simplified real-world use of forceUpdate. Something like:

  1. Create a component whose output depends on something other than props/state
  2. Render
  3. Change the external state
  4. Force update
  5. Check that the rendered output reflects the updated external state.

});

it('should add callback to renderCallbacks', () => {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

renderCallbacks is an implementation detail which we should avoid in the description. What the test actually does, checking that forceUpdate invokes its callback, is fine though.

let forceUpdate;
let callback = sinon.spy();
class ForceUpdateComponent extends Component {
componentDidMount() {
forceUpdate = () => this.forceUpdate(callback);
}
render() {
return <div />;
}
}
sinon.spy(ForceUpdateComponent.prototype, 'forceUpdate');
render(<ForceUpdateComponent />, scratch);

forceUpdate();

expect(ForceUpdateComponent.prototype.forceUpdate).to.have.been.called;
expect(ForceUpdateComponent.prototype.forceUpdate).to.have.been.calledWith(callback);
expect(callback).to.have.been.called;
});
});
});