Repository navigation
Replies: 1 comment
|
The reaction scheduler schedules a pending reaction; it is not called again for every change while that reaction is already pending. A debounce placed there therefore does not keep restarting its timer. For a trailing debounce, let the reaction track each change normally and debounce its effect: import { observable, reaction } from 'mobx';
import debounce from 'lodash/debounce.js';
const search = observable({ query: '' });
const commit = debounce(query => console.log(query), 100);
const stop = reaction(
() => search.query,
query => commit(query),
);Update I checked this with MobX 6.15.0/7.0.6 and Lodash 4.17.21 using real timers: changes within the debounce interval produced one effect with the latest query, and cancellation prevented a pending effect. A separate scheduler probe confirmed that multiple changes while pending scheduled one run. See reaction scheduling. |
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
I've assigned
debounceas a reaction scheduler:I thought the
runwill be invoked after X ms when there are no actions.But it doesn't work as expected. It is invoked every X ms even if there are many action in X ms. It's like throttling!
How does MobX decide to call reaction scheduler?
And I wanna debounce schedulers. How can I do that?
Thank you.
All reactions