VueJs child component props not updating instantly

Christof picture Christof · Sep 26, 2016 · Viewed 48k times · Source

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()
    }
}

Answer

J. Bruni picture J. Bruni · Sep 27, 2016

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
  }
}