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

141
Views
How to correctly share reactive variables setted on onMounted hook in Vue 3's composition API?

please read the following (wrong) code. How can I fix it?

My problem is that I'm trying to share several variables, which should be reactive, between different composables and the main app.

All those variables need DOM to be assigned, thus, they need onmounted hook. So useDropzones and usePanels both assign variables onmounted as well as the main app. And all those variables are cross-used between the cited entities. But in fact, they will be all undefined, quite obviously. How to fix?

const vueapp = Vue.createApp({

  setup(props, {attrs, slots, emit}){

    let validators = reactive([]);
    let panels = reactive([]);
    let code = null;

    useDropzones(code); // assigned by main app's onMounted
    usePanels(panels, validators); // it will populate panels, onmounted, used by main app below, onmounted

    onMounted(() => {
      nextTick(() => {

        code = form_elem.find('input[name="form-code"]').val(); // here I'm setting code variable, used by useDropzones()

        _.each(panels, function(panel, index){ // TODO: native?
          // execute code that populates validators, used by usePanels()
        }.bind(this));

      }); // nexttick
    }); // onmounted

    return {}
  },

});
about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

Move the useDropzones(code) call into onMounted() when you've set code to the element reference:

Vue.createApp({
  setup() {
    // ⛔️ moved into onMounted
    // let code = null;
    // useDropzones(code);

    onMounted(() => {
      nextTick(() => {
        const code = form_elem.find('input[name="form-code"]').val();
        useDropzones(code);
      })
    })
  }
})
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!