Challenge: Reuse Data Objects with Alpine.data

This challenge will test your knowledge on how to create a reusable AlpineJS component.

We'll cover the following

Challenge yourself

Convert the following component to a reusable AlpineJS component using Alpine.data. Name the component collapsible and maintain all elements, properties, and methods.

<div x-data="{
  open: true, 
  get isOpen() { return this.open },
  toggle() {
    this.open = !this.open;
  }}">
  <h4 @click="toggle">Toggle</h4>
  <p x-show="isOpen">
    I am collapsible
  </p>
</div>

Note: Put the reusable Alpine.data component definition into the “JavaScript” tab.

Get hands-on with 1200+ tech skills courses.