Nanny State: A streamlined Vanilla JS state management library
Nanny State is a mini library designed to simplify the process of building state-based web applications using native JavaScript. It's similar to React, but with less overhead, no need to learn new syntax. It uses a single application-wide state object instead of letting each component have its own state. It was inspired by HyperApp and has many similarities with Elm.
This article will explain how Nanny State works and demonstrate its functionality with several examples.
Key points:
One-way data flow
Nanny State uses a one-way data flow model, consisting of three parts:
In Nanny State, state is everything. The state object is the only source of fact for the application—every part of the application’s data is a property of this object. Even event handlers used in views are properties of state objects.
View is a representation of state as HTML. It changes when the state changes and allows the user to interact with the application.
Update functions is the only way to change state. It is a single entry point for updating the state and ensures that changes are deterministic, consistent, and predictable.
Building a Nanny State application requires only these three parts. In fact, it can be summarized as follows three questions:
Hello Nanny State!
The easiest way to understand how Nanny State works is to write some code! We'll start with a basic example and try to make something more complex.
The easiest way to run the following example is to use an online code editor such as CodePen, or you can run it locally by installing the nanny-state package using NodeJS.
Copy the following code to the JS part of CodePen:
import { Nanny, html } from 'https://cdn.skypack.dev/nanny-state'; const View = state => html`<h1>Hello ${state.name}</h1>`; const State = { name: "Nanny State", View }; const Update = Nanny(State);
This shows how the three parts of Nanny State work together. Let's take a closer look at each section:
const View = state => html`<h1>Hello ${state.name}</h1>`;
Nanny State uses µhtml to render HTML. The view function Always accepts a state object as its unique parameter. It then uses the html function provided by µhtml to create HTML based on the template literals provided as parameters.
Using template literals means we can insert the properties of the state into the view using the ${variable}
symbol. In this example, we use it to insert the value of the name attribute into the <h1>
element.
const State = { name: "Nanny State", View };
State objects are where all application data is stored. It contains any attributes and values that will be displayed in the view that may change over the life of the application, such as the name attribute in this example.
Please note that View is also a property of State using object abbreviation notation. Remember Status is everything - Every part of the application is a property of the state.
const Update = Nanny(State);
The last line defines the update function as the return value of the Nanny function. This can now be used to update the value of any property of State. In fact, this is the only way to update any property of State. It also performs initial rendering of the View based on the values provided in State. This means a title will be displayed with the words "Hello Nanny State" as shown in the CodePen below:
...(The subsequent content is similar to the original text, except that the language and expression are adjusted, the original text is kept unchanged, and all pictures and their formats are retained.)
The above is the detailed content of Simple State Management in JavaScript with Nanny State. For more information, please follow other related articles on the PHP Chinese website!