wtnbass/fuco

How to force a re-render?

Closed this issue · 2 comments

Nice library, thanks for creating it.

I want to re-render my component based on some condition, such as an updated in an imported reactive store. In lit-element I would call this.requestUpdate() to do that.

How do I request an update (re-render) in fuco?

You can use a reducer that always returns a different value with useReducer to force update.
Here is an example:

const [, forceUpdate] = useReducer<number, void>(x => x + 1, 0);

function subscribe() {
  forceUpdate();
}

Reference

https://reactjs.org/docs/hooks-faq.html#is-there-something-like-forceupdate

Thanks, that worked very well.