Skip to content
Tutorials Center
My learning

Topics

Lesson 3 of 28 ยท Alpine.js Basics

Reactive State with x-data

Learn Alpine.js state: x-data objects, x-text, x-show, @click, methods, getters, component scope, several components per page and x-init.

Alpine.js is built on one idea: you declare a bit of state on an element, and everything inside that element can read it, show it and change it. When the state changes, the page updates itself. You never write document.querySelector, and you never copy values into the DOM by hand.

The directive that creates state is x-data. This lesson covers it and the first handful of directives you use with it. Make sure you have the script tag from Setting Up; every example below already includes it.

x-data: declaring a component

Put x-data on an element and give it a JavaScript object. That element becomes an Alpine component, and the object becomes its reactive state. Two directives show the state:

  • x-text sets the element's text to the value of a JavaScript expression.
  • @click (short for x-on:click) runs an expression when the element is clicked.
A counter in three attributes

Everything inside the quotes is plain JavaScript. count++ changes the property, and Alpine notices and updates every place that reads count. This automatic tracking is called reactivity.

How Alpine reacts

State x-data holds the values.
Directives x-text and x-show read them.
Events @click changes them.

Change the state and the page follows.

1 / 2

Showing and hiding with x-show

x-show takes an expression. When it is truthy the element is visible, and when it is falsy Alpine sets display: none on it. The element stays in the DOM, so toggling is cheap.

A toggle with x-show

Notice that x-text can hold any expression, including a ternary. Keep those short. When they grow, use a getter or a method (see below).

Methods and getters

Your state object is a normal object, so it can contain methods (functions) and getters (computed values). Methods are for actions, getters are for derived values that you read like properties. Inside them, this refers to the state.

Methods and a getter

Two details to remember. A getter is called without parentheses (total), a method with them (add()). And inside an attribute you may use the property names directly (quantity), while inside the object's own functions you write this.quantity.

A long x-data object makes the HTML hard to read. When it grows beyond a few lines, move it into a reusable function with Alpine.data, covered in Reusable Components, Alpine.data and Stores.

Scope: who can see the state

State belongs to the element that declares it and all of its descendants. Siblings and parents cannot see it. A page can hold as many components as you like, and each has its own independent copy.

Two independent components

Both components use the name likes, yet they never interfere. Components can also be nested: an inner component can read its parent's state, but it can also shadow a name with its own. Keep nesting shallow; it is easy to lose track of which count you mean.

x-init: running code at startup

x-init runs an expression once, when Alpine initializes the component. Use it to set up values that need computing, to read the DOM, or to start a timer.

x-init and a timer

You can also define an init() method inside the x-data object. Alpine calls it automatically, and it is the cleaner choice once the setup code has more than one line.

Common mistakes

  • Forgetting that x-data is required. Without an ancestor x-data, directives like x-text throw "variable is not defined" errors in the console.
  • Using a string where you need an expression. x-text="Hello" looks for a variable named Hello. To show a literal, write x-text="'Hello'" (a string inside quotes) or just put the text in the HTML.
  • Reading state from a sibling. If two parts of the page need the same value, wrap them in a common component, or use a store (later).
  • Mixing quote styles. The attribute uses double quotes, so strings inside use single quotes.

Check your understanding

Quick check ยท 4 questions

of 4 answered You scored / 4 ยท perfect!

  1. 1. Which attribute creates reactive state for an element and its children?

    x-data declares the state object. The other directives read or react to it.
  2. 2. What does x-show="false" do to an element?

    x-show only toggles the display style. To add and remove the element itself, use x-if on a template.
  3. 3. How do you read a getter named total in an attribute?

    A getter behaves like a property, so you write its name without parentheses.
  4. 4. Two sibling components both declare { count: 0 }. What happens when you change one?

    Each x-data creates an independent copy of the state, scoped to its own element and descendants.

Summary

  • x-data="{ ... }" turns an element into a component with reactive state.
  • x-text writes a value into an element; x-show toggles display; @click runs code on click.
  • State objects can hold methods (actions) and getters (derived values); use this inside them.
  • State is visible to the declaring element and its descendants only; each component is independent.
  • x-init (or an init() method) runs setup code once.

Continue with Binding Attributes, Classes and Styles.