Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

193
Views
how to dispatch param using x-init in Alpinejs

My Alpinejs component code is in an EventListener and I wish to use the magic $dispatch to pass a title string from outside the component. I can only achieve this using an onclick event but really need to do this on page load.

This is the code outside of the component used to pass in the title using @click. This works but it needs to be set when the page is loaded (in Alpinejs I believe this is x-init)


<div x-data>
  <button 
    @click="$dispatch('myparams', { title: 'The title' })"
    type="button" 
  >
    More info
  </button>
</div>

Using x-init does not work with the below

<div 
  x-data 
  x-init="() => $dispatch('myparams', {'title': 'Initialised'})"
// x-init="$dispatch('myparams', {'title': 'Initialised'})"
>
</div>

Here is my component with what I've tried to receive data from outside

<section 
  x-data="mypage"
  @myparams.window="title = $event.detail.title"
>
  <h2 x-text="title"></h2>
</section>

<script>
document.addEventListener('alpine:init', () => {
    Alpine.data('mypage', () => ({
      title: '',
      init() {
        @myparams.window="title = $event.detail.title"
        console.log('I will get evaluated when initializing this component.')
      },
    }))
})
</script>

about 4 years ago · Juan Pablo Isaza
2 answers
Answer question

0

The reason of this behavior is that you dispatch the myparams event before the initialization of listener for myparams event. So the first time no one is listening.

The future-proof solution is to use $nextTick so the dispatch fires after DOM updates.

<div x-data="initMyparams"></div>

<section x-data="mypage" @myparams.window="title = $event.detail.title">
  <h2 x-text="title"></h2>
</section>

Where we have a new initMyparams component using $nextTick:

<script>
document.addEventListener('alpine:init', () => {
    Alpine.data('mypage', () => ({
        title: '',
        init() {
            console.log('I will get evaluated when initializing this component.')
        },
    })),

    Alpine.data('initMyparams', () => ({
        init() {
            this.$nextTick(() => this.$dispatch('myparams', { title: 'Initialised' }))
        },
    }))
})
</script>
about 4 years ago · Juan Pablo Isaza Report

0

Managed to figure it out. Basically All I needed to do was include the component on the page BEFORE the onload (x-init) event.

about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!