Vue Js : Passing Data among siblings - vuejs2

Is there any other way to pass data between 2 siblings without using event bus or parent component in vue js?
I know that it can be passed with the help of Props and event emission. It can also be passed with the help of event buses.

If it is complex you would go you Vuex is the best solution in my opinion. However, if you just want a simple way to communicate between the components, you can simply go for OO Shared State. This is just a class that maintain your data that you can access from any components just pass it though, similar to singleton concept it OOP. There is a good example how to do this in Laracast website; have a look here https://laracasts.com/series/learn-vue-2-step-by-step/episodes/24.

A general question is going to get a general answer. You need to ask yourself why your siblings need to talk to each other. Most often it's because you're not using a store. It would be a mistake to think of vue components like objects in an OO language. Vue components are fed via reactive pipes from a store, the store is not a vue component. If you're doing this correctly, you don't often need to pass stuff between vue components.

Related

Provide/inject or Vuex?

I have a list of cars. Each item of that list has all the information about some of the cars. Also each item is a parent component to many other components - image, price, characteristics, etc - all that info is in separate components.
The information about all those cars is stored in Vuex. Now I am trying to think of a better way to pass that information to each of the list items.
Pass all the necessary data about another car to the parent component. And then provide it to the child components.
Each child component gets the necessary information from Vuex directly.
Provide-inject is mainly used to pass data to nested components.
Vuex on the other hand, keeps the app state shared.
You need to ask yourself whether the data you need in your component is coming from a parent-parent component or the data is used in many components in different parts of the app. If you need the first, then go with the provide-inject, otherwise, pick Vuex.
Both approaches are prone to be badly implemented and potentially impact negatively on your app development and maintainability, so be very careful and learn the basics in-depth ;)

Vuex and vue best practise questions

1 I see a some times that a vuex action collects information from other vuex modules using dispatch from other modules. With this it looses its "modulair" flexibility. What is the best practise in this case?
2 Also do you agree that Vue components needs to be kept clean from alot of fetch and formcheck logic and better move this logic to vuex?
isolate the common code between 2 modules into a pure function, then import this function in both modules. You can even use a Class with static methods.
yes, Vue components should be a pure presentation/view layer - the logic must stay into reusable non-visual classes. The form validation checks are especially suitable for pure functions. And fetches could be extracted as static methods into separate Service class (or several such classes, if you want to follow the Modules pattern).

When to use functional components in Vue.js?

I have just read about functional components in Vue.js 2. (Documentation)
The major benefits of function components seems to be performance improvement:
Since functional components are just functions, they’re much cheaper
to render.
It seems like that for emitting events from functional components an event listener has to be added manually. However props are fully accessable.
What is a good rule of thumb when to use a functional component instead of a class component? Should I prefer using the functional components when I implement a stateless component in general? Or are there any other limitations to functional component in Vue.js?
I was just thinking about this the other day. ..
There are some additional caveats to functional components (e.g. passing classes requires custom handling How to apply classes to Vue.js Functional Component from parent component?)
The rule of thumb I'd use is when the component is when you are using many instances of the component, (so you get a tangible performance benefit) and preferably it's not too complicated in terms of interaction (since you lose most Vue magic)

In Mobx can a complex component have it's own store?

I have built a complex date management component that itself is made up of many smaller components nested a few layers deep. The components at different levels need access to observables and actions that live in the root component. I am dealing with this by passing what I need through props. While this all works, the code would be much simpler and easier to follow if I could create a store in the root component, use provider, and then inject in the child components as needed. Exactly like we do when creating a store for app wide use.
So to summarize...
I want each instance of my complex date component to create it's own instance of a store. I want to then use provider and inject to pass this store instance to child components.
Before I proceed with this significant refactoring, I want to be sure that this approach will work and is sound. Is there an alternative approach that makes more sense. Am I overlooking something?
Note: Although I know code samples are useful, I'm hoping that what I am describing is straight forward enough that they are not needed in this case.
Yes, you can create stores for a component and use Provider and inject to pass them to child components. Actually that is what I do in my current project and it works well.
BTW, I think this is a big Pro of MobX compared to Redux. You do not need to keep all states in a single global store. You can create complicated components that work perfectly by themselves. It is object-oriented, which is the philosophy of React.

What is a good pattern to access other components' states?

I encounter a situation where component A needs to know about the state of component B when A is asked to perform certain actions (for example, if edit menu is toggled, the save action on the save button should not be performed). My application is structured like a tree with nested components.
I have tried passing all the components I need into the constructor of other components. I find this quite tedious whenever I add more component to the application, I have to pass them all the way down. Furthermore, some components are instantiated under the same constructor, but they need to know about each other. I cannot pass say component A and B into each other since I need to instantiate them in order.
I have also tried using event system to signal between components. (Observer pattern ?) It seems to be more of an overkill and not intended to be used like this.
3rd thing I try is to use singleton through dependency injection. Components register themselves on init to the provider and the provider can be injected to provide access to other components.
The 3rd approach is the most effortless and it is working for me. But I google that Singleton is not a recommended approach since it is just global variable and it entangles the code. But Unity game engine seems to have the same thing ( FindComponentByTag ). What is the general practice for this ?
The standard pattern for handling things like this is MVC (Model View Controller) which usually makes use of the Observer pattern. Components (I guess that you are having GUI components in mind) should not directly access the state of other components. Instead, state should be handled by the model. The components that need to know about the state are registered as observers of the model.