-
Book Overview & Buying
-
Table Of Contents
-
Feedback & Rating
Micro State Management with React Hooks
By :
Here, we'll learn about the Subscription mechanism and how to connect a module state to the React state.
Subscription is a way to get notified of things such as updates. A typical use of a Subscription would look like the following:
const unsubscribe = store.subscribe(() => {
console.log('store is updated');
});
Here, we assume a store variable to have a subscribe method that takes a callback function and returns an unsubscribe function.
In this case, the expected behavior is that whenever store is updated, the callback function is invoked and it shows the console log.
Now, let's implement a module state with a Subscription. We'll call it store, which holds the state value and the subscribe method, in addition to the getState and setState methods that we described in the Exploring the module state section. A createStore is a function to create store with an initial state value:
type Store<T>...