Run Code on Element Initialization: x-init

Learn to run code during AlpineJS component initialization.

The x-init directive

There are times we may want to run code before AlpineJS renders its components. For example, our component may need to get some data from the network or check some data before rendering. AlpineJS allows us to hook into a component’s initialization with the x-init directive.

Get hands-on with 1200+ tech skills courses.