State is the current data an application uses to decide what to display and do. It might be a counter, a search query, a list of products, or whether a filter is enabled. When state changes, the parts of the interface that depend on it should update predictably. section 13.1
Keep three jobs distinct: state holds current data; derived data is computed from it; effects do work such as updating the DOM, saving to storage, or sending a request. A practical flow is user event → update state → compute derived data → update the DOM. The flow does not require a framework runtime. section 13.1
For a small form or dashboard, start with a plain state object and a render function. An input handler writes values into state; rendering reads state and writes the resulting values into the DOM. These are two explicit directions, not an automatic binding loop. section 13.2.3
const state = {
query: "",
onlyAvailable: false,
allProducts: [],
};
function visibleProducts() {
return state.allProducts.filter((product) => {
const matchesText =
product.name.toLowerCase().includes(state.query.toLowerCase());
const matchesStock =
!state.onlyAvailable || product.available;
return matchesText && matchesStock;
});
}
function renderProductRow(product) {
const row = document.createElement("li");
row.textContent = `${product.name} — $${product.price}`;
return row;
}
function render() {
const matches = visibleProducts();
count.textContent = `${matches.length} products`;
searchInput.value = state.query;
availableCheckbox.checked = state.onlyAvailable;
productList.replaceChildren(...matches.map(renderProductRow));
}
The product-filter example stores the query, stock filter, and products in state. It computes the visible products separately, then renders the count, controls, and list from those values. section 13.2.3
FormData can make form-to-state updates concise, but check the shape of each field. A checkbox can be read with has(); a repeated field needs getAll() rather than an assumption that Object.fromEntries() preserves every value. section 13.2.3
If one action changes several state properties, schedule one render instead of rendering after every assignment. A queueMicrotask() callback can batch those writes; a guard prevents duplicate render requests in the same synchronous operation. Use this when the simpler direct render call would repeat work. section 13.2.3
State also describes conditions beyond form values. A data request can have explicit UI states across its lifecycle, and interactive components may need stable default, focus, disabled, error, success, or loading states for visual tests. Make those conditions identifiable rather than leaving the UI's condition implicit. section 12.0 section 9.7.4



















































