Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

VueJs child component props not updating instantly

I have a parent/child component setup where the parent is loading data from the server and passing it down to children via props. In the child I would like to instantiate a jQuery calendar with some of the data it receives from the parent.

In order to wait for the data before setting up the calendar, I broadcast an event in the parent that I have an event listener setup for in the child.

The listener is being fired in the child but if I this.$log('theProp'), it's undefined. However, if I inspect the components with the VueJs devtools, the parent/child relationship is there and the child has received the prop in the meantime.

The prop is defined on the child as a dynamic prop :the-prop="theProp". Since the child does receive the prop in the end, I'm assuming my setup is correct but there seems to be some sort of delay. The parent sets the props in the return function of the ajax call and again: it's working, just with a slight delay it seems.

I also tried registering a watch listener on the prop in the child so I could setup the calendar then and be sure that the prop is there. However, the watch listener fires, but this.$log('theProp') is still undefined.

If I pass the data along with the the broadcast call, like this.$broadcast('dataLoaded', theData) the child receives it just fine. But it seems wrong to do it that way as I'm basically building my own prop handler.

I'm not posting any code because the components are rather large and the VueJs devtools are telling me the parent/child situation is working.

Am I missing some information? Is there a slight delay between setting a value in the parent and the child receiving it? What would be the proper way to wait for parent data in the child?

Normally, when you're just rendering the data out into the template, the timing doesn't matter so much since the data is bound to the template. But in this case, I really need the data to be there to setup the calendar or it will be wrong.

Thanks.

edit 1: here's a jsfiddle: https://jsfiddle.net/dr3djo0u/1/ It seems to confirm that the data is not available immediately after the broadcast. However, the watcher does work, though I could almost swear that sometimes this.$log('someData') returned undefined when I setup that testcase.

But I guess my problem might be somewhere else, I'll have a look tonight, don't have the project with me right now.

edit 2: did some more tests. My problem was that a) event listeners do not seem to receive the data instantly and b) I was also trying to init the calendar in the route.data callback if someData was already around (e.g. when coming from parent), but that route callback is called before the component is ready, so it wasn't working there either.

My solution is now this:

// works when the child route is loaded directly and parent finishes loading someData
watch: {
    someData() {
        this.initCalendar();
    }
},

// works when navigating from parent (data already loaded)
ready() {
    if (this.someData && this.someData.length) {
        this.initCalendar()
    }
}
like image 797
Christof Avatar asked Sep 26 '16 07:09

Christof


3 Answers

As far as I know, you should not need events to pass data from parent to child.

All you need is, in the child component: props: ['theProp']

And when using the child component in the parent: <child :theProp="someData"></child>

Now, wherever in the parent you change someData, the child component will react accordingly.

You don't need events, you don't need "watch", you don't need "ready".


For example: after an AJAX call, in the parent's "ready", you load some data:

// at the parent component

data: function () {
  return {
    someData: {}
  }
},

ready: function () {
  var vm = this;
  $.get(url, function(response) {
    vm.someData = response;
  });
}

Now, you do not need anything else to pass the data to the child. It is already in the child as theProp!

What you really need to do is to have, in the child, something which reacts to data changes on its own theProp property.

Either in the interface:

<div v-if="theProp.id > 0">
  Loaded!
</div>

Or in JavaScript code:

// at the child component

computed: {
  // using a computed property based on theProp's value
  awesomeDate: function() {
    if (!this.theProp || (this.theProp.length === 0)) {
      return false;
    }
    if (!this.initialized) {
      this.initCalendar();
    }
    return this.theProp.someThing;
  }
}

Update 1

You can also, in the parent, render the child conditionally:

<child v-if="dataLoaded" :theProp="someData"></child>

Only set dataLoaded to true when the data is available.

Update 2

Or maybe your issue is related to a change detection caveat

Maybe you're creating a new property in an object...

vm.someObject.someProperty = someValue

...when you should do...

vm.$set('someObject.someProperty', someValue)

...among other "caveats".

Update 3

In VueJS 2 you are not restricted to templates. You can use a render function and code the most complex rendering logic you want.

Update 4 (regarding OP's edit 2)

Maybe you can drop ready and use immediate option, so your initialization is in a single place:

watch: {
  someData: {
    handler: function (someData) {
      // check someData and eventually call
      this.initCalendar();
    },
    immediate: true
  }
}
like image 53
J. Bruni Avatar answered Oct 17 '22 05:10

J. Bruni


It's because tricky behavior in Vue Parent and Child lifecycle hooks.

Usually parent component fire created() hook and then mount() hook, but when there are child components it's not exactly that way: Parent fires created() and then his childs fire created(), then mount() and only after child's mount() hooks are loaded, parent loads his mount() as explained here. And that's why the prop in child component isn't loaded.

Use mounted() hook instead created()

like that https://jsfiddle.net/stanimirsp5/xnwcvL59/1/

like image 27
stanimirsp Avatar answered Oct 17 '22 05:10

stanimirsp


Vue 3

Ok so I've spent like 1.5h trying to find out how to pass prop from parent to child:

Child

<!-- Template -->
<template>
  <input type="hidden" name="_csrf_token" :value="csrfToken">
  <span>
    {{ csrfToken }}
  </span>
</template>

<!-- Script -->
<script>
export default {
  props: [
    "csrfToken"
  ]
}
</script>

Parent

<!-- Template -->
<template>
  <form @submit.prevent="submitTestMailForm" v-bind:action="formActionUrl" ref="form" method="POST">
...
    <CsrfTokenInputComponent :csrf-token="csrfToken"/>
...
  </form>
</template>

<!-- Script -->
<script>
...
export default {
  data(){
    return {
      ...
      csrfToken : "",
    }
  },
  methods: {
    /**
     * @description will handle submission of the form
     */
    submitTestMailForm(){
      let csrfRequestPromise = this.getCsrfToken();

      let ajaxFormData = {
        receiver     : this.emailInput,
        messageTitle : this.titleInput,
        messageBody  : this.bodyTextArea,
        _csrf_token  : this.csrfToken,
      };

      csrfRequestPromise.then( (response) => {
        let csrfTokenResponseDto = CsrfTokenResponseDto.fromAxiosResponse(response);
        this.csrfToken           = csrfTokenResponseDto.csrToken;

        this.axios({
          method : "POST",
          url    : SymfonyRoutes.SEND_TEST_MAIL,
          data   : ajaxFormData,
        }).then( (response) => {
          // handle with some popover
        })
      });
    },
    /**
     * @description will return the csrf token which is required upon submitting the form (Internal Symfony Validation Logic)
     */
    getCsrfToken(){
      ...
      return promise;
    }
  },
  components: {
    CsrfTokenInputComponent
  }
}
</script>

Long story short

This is how You need to pass down the prop to child

    <CsrfTokenInputComponent :csrf-token="csrfToken"/>

NOT like this

    <CsrfTokenInputComponent csrf-token="csrfToken"/>

Even if my IDE keep me telling me yeap i can navigate with that prop to child - vue could not bind it.

like image 23
Volmarg Reiso Avatar answered Oct 17 '22 05:10

Volmarg Reiso