How to update the value of child component's props from parent component?

drake035

From my parent component, I need to trigger a child component's portion of code containing a Firebase query. Note that in the child component, that code is triggered within the mounted() hook (but in order to execute it from the parent component, I suppose I will have to move that code to a method or something).

What is the recommended way to achieve such a thing?

Here's my parent component code - simplified for clarity:

<template>
  <div>
    <nav>
      <v-btn @click="viewNextWeek"></v-btn>
    </nav>
    <project-row :mon="mon"></new-row-form>
  </div>
</template>

<script>
import ProjectRow from './ProjectRow.vue';
import store from '../store';
import moment from 'moment';

export default {
  name: 'home',
  components: {
    ProjectRow
  },
  data() {
    return {
      mon: moment().startOf('isoWeek')
    }
  },
  methods: {
    viewNextWeek: function() {
      this.mon = moment().startOf('isoWeek').add(7, 'days');
    }
  }
}
</script>

Child component - simplified too:

<template>
  <div>
    <input v-model="monTime">
  </div>
</template>

<script>
import { db } from '../firebase';

export default {
  props: [
    'mon'
  ],
  mounted() {
    var timesRef = db.collection('times');
    var timesWeek = timesRef.where('date', '==', this.mon);
    timesWeek.get()
    .then((querySnapshot) => {      
      querySnapshot.forEach((doc) => {
        this.monTime = doc.data().time;
      });
    })
    .catch(function(error) {
      console.log('Error getting documents: ', error);
    });  
  }
}
</script>
Thamer

to achieve that you should use event bus communication between these two components. first, you should create a global event bus that can be a central event management and will give you the possibility to access any event from any component within your vuejs app. in your case here, you should emit an event from your parent component within viewNextWeek method and catch it within mounted in child component. so every time your mon value change within your parent component, will trigger an event to the child component to update mon within it.

for more detail for how to use global event bus, read this article global event bus article

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

How to store the parent's props in child component?

How to update child component after props are changed in parent component

Where to setState the props value coming from parent component in child component?

How to update child component’s props?

Send props from parent to child and update them in child component (ReactJs)

Child component not update value from Parent

How do I reset props from a parent component in a child component?

How to update parent's variable from a child component

How update state in parent component from child, child component?

Angular 5 Update Parent Component value from child Component

Angular to update UI from the child component reflect the value to the parent component

Update property value of child component from parent component

How to pass props from child component to its parent of parent?

How to update the correct state value in a parent component that gets its value from a child component using hooks in react?

How to update parent data after filter props in child component?

Async update parent props to child component

React Parent To Child Component Props Update Issue

How to access child component's child from parent component in react?

React child component not rendering props from parent value

How to pass props from child component to parent component to another child component in ReactJS?

How to pass value from child to parent component

How to pass a value from a child to a parent component?

How To Update State In Parent Component From Child Component In React Native?

how to update parent component from child component in angular2

How to update parent component from child component in Angular 2

How to force update child component from parent component in reactjs

Update child component from parent

Update parent component from child component

Update parent React component from child component