mirror of
https://github.com/nerfstudio-project/nerfstudio.git
synced 2026-11-12 19:04:03 +08:00
20 lines
571 B
JavaScript
20 lines
571 B
JavaScript
import { useContext } from 'react';
|
|
import { ReactReduxContext } from 'react-redux';
|
|
|
|
export function subscribe_to_changes(selector_fn, fn) {
|
|
// selector_fn: returns a value from the redux state
|
|
// fn_valid: function to run on a valid input
|
|
// fn_null: function to run on a null input
|
|
const { store } = useContext(ReactReduxContext);
|
|
|
|
let current;
|
|
const handleChange = () => {
|
|
const previous = current;
|
|
current = selector_fn(store.getState());
|
|
if (previous !== current) {
|
|
fn(previous, current);
|
|
}
|
|
};
|
|
store.subscribe(handleChange);
|
|
}
|