

State management in its own module in vanilla JavaScript
source link: https://dev.to/chovy/state-management-into-its-own-module-in-vanilla-javascript-58mf
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.


State management in its own module in vanilla JavaScript
You can organize your code better by separating the state management into its own module. Here's how you might do it:
In store.js:
// Define the initial state
let state = {
count: 0
};
// Define a list of subscribers
let subscribers = [];
// Define a function to update the state and notify subscribers
function setState(newState) {
state = newState;
// Notify subscribers
for (let i = 0; i < subscribers.length; i++) {
subscribers[i](newState);
}
}
// Define a function to subscribe to state changes
function subscribe(callback) {
subscribers.push(callback);
}
// Export the state, setState, and subscribe functions
export { state, setState, subscribe };
In main.js (or wherever you want to use the state):
import { state, setState, subscribe } from './store.js';
// Subscribe a function that updates the DOM to state changes
subscribe(function(newState) {
document.getElementById('count').textContent = "Count: " + newState.count;
});
// Define a function to increment the count
function incrementCount() {
setState({
count: state.count + 1
});
}
// Call the function to increment the count
incrementCount();
In this example, store.js
exports the current state, a setState function to update the state, and a subscribe function to subscribe to state changes. Then, in main.js, you import these exports and use them to manage the state of your application.
Please note that this example uses ES6 modules, which may not be supported in all environments without a build step (like Babel or webpack). You might also need to run this code on a server (like using the http-server
package in Node.js), as some browsers don't support ES6 modules with the file://
protocol, which is typically used when you open an HTML file directly in your browser.
If you're looking for a remote job working with javascript, signup for Grazily.com (it's free)
Recommend
-
5
everybody's a CPU designer these days — Microsoft may be developing its own, in-house ARM CPU designs Bloomberg's unconfirmed report relies on confidential sources within Microsoft....
-
8
Good software propagates its own correctness Apr 26, 2018 bitcoin...
-
16
Moving a git repository subdirectory to its own repository Wednesday, November 4, 2009 I use Git and GitHub to manage all of my personal code projec...
-
10
Woo for its own sake Posted in Design, tools by Scott Locklin on January 8, 2021 ...
-
10
Xiaomi Reportedly Planning to Build Its Own Car- PingWestXiaomi Reportedly Planning to Build Its Own Car February 20, 2021 0:38 am Beijing (PingWest)—Chinese smartphone manufacturer Xiaomi plans to tap into the car manufacturing busine...
-
6
SEM Google announces its own version of App Tracking Transparency When users opt out, advertisers “will receiv...
-
7
-
15
Netflix working on its own gaming platform As companies have been investing in new solutions for game subscription services, it seems that Netflix will soon join this club. According to a new report from
-
7
-
6
Oct 2, 2022 · #react #typescript #stateHow to write your own state management library
About Joyk
Aggregate valuable and interesting links.
Joyk means Joy of geeK