0

I have a question about passing variables to nested components.

I have 3 components:

  1. Parent
  2. Child
  3. SubChild

and they have a shared variable (call it myVar).

Suppose I don't want to use Vuex. My question is what is a better way of passing myVar from Parent to SubChild.

First way

  1. (In Parent) I am passing myVar to Child by props option. (it's common).
  2. (In Child) I am passing the prop option myVar directly to SubChild as its prop option.

Second way

  1. (In Parent) I am passing myvar to Child by props option. (it's common).
  2. (In Child) I am cloning the prop option myVar to local variable (call it myLocalVar) and passing the cloned variable myLocalVar to SubChild as its prop option.

I prefer the second way, but I have to create watches

  1. To handle changes from Parent to myLocalVar.
  2. To handle changes from Child to emit it to Parent.
3
  • 1
    I don't see where the question of your question is, but, if your aplication grows, it's better to use Vuex. Other than that the first way looks more clean, but can be dificult to watch the changes and pass it to parents/childs. Commented Aug 30, 2021 at 12:25
  • imo you should use vuex, or create a mixin that has a Vue.observable object in data, then all which import the mixin would have a shared object, props are for component options not data, both your options will bloat the components with input/output logic for the prop Commented Aug 30, 2021 at 12:25
  • 1
    Provide / inject Commented Aug 30, 2021 at 12:50

3 Answers 3

2

To do this you can use provide/inject. Your parent would provide data like this:

provide: {
    user: 'John Doe'
  }

And your nested child would use it like this:

inject: ['user']

Nethertheless you should really use Vuex for this.

Sign up to request clarification or add additional context in comments.

Comments

2

I would have written this as a comment, but I don't have enough "reputation".

As Mael pointet out in his/her comment, your posting does not contain a question. You are merely describing two (working) implementation patterns.

Assuming that you want to know which of it is a better choice I would say:

I'd prefer the "First way" as it's cleaner and needs less code.

The "Second way" seems only be useful when you need to map the property to a different value for the SubChild.

But in this case I would not use watchers. Instead use a computed value that you can pass to the SubChild as a property.

Whenever the property given by the Parent is changed the computed value in the Child should be recalculated and thus trigger the SubChild re-render.

Comments

1

You shouldn't pass it all you should have a callback function in the parent that can change the state in the parent. The only place you should actually have state is the parent.

Then you can pass this function as props as far down as you want (or as noted here provide/inject), this way you have 1 place you keep your state and 1 function that changes it. Any event that should change the state will call the callback and it will change at the parent level.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.