You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: README.md
+201-1Lines changed: 201 additions & 1 deletion
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -160,6 +160,19 @@ List of 300 VueJS Interview Questions
160
160
|151|[What is mapState helper?](#what-is-mapstate-helper)|
161
161
|152|[How do you combine local computed properties with mapState helper?](#how-do-you-combine-local-computed-properties-with-mapstate-helper)|
162
162
|153|[Do you need to replace entire local state with vuex?](#do-you-need-to-replace-entire-local-state-with-vuex)|
163
+
|154|[What are vuex getters?](#what-are-vuex-getters?)|
164
+
|155|[What is a property style access?](#what-is-a-property-style-access)|
165
+
|156|[What is a method style access?](#what-is-a-method-style-access)|
166
+
|157|[What is mapGetter helper?](#what-is-mapgetter-helper)|
167
+
|158|[What are mutations?](#what-are-mutations)|
168
+
|159|[How do you commit with payload?](#how-do-you-commit-with-payload)|
169
+
|160|[What is object style commit?](#what-is-object-style-commit)|
170
+
|161|[What are the caveats with vuex mutations?](#what-are-the-caveats-with-vuex-mutations)|
171
+
|162|[Why mutations should be synchronous?](#why-mutations-should-be-synchronous)|
172
+
|163|[How do you perform mutations in components?](#how-do-you-perform-mutations-in-components)|
173
+
|164|[Is it mandatory to use constants for mutation types?](#is-it-mandatory-to-use-constants-for-mutation-types)|
174
+
|165|[How do you perform asynchronous operations?](#how-do-you-perform-asynchronous-operations)|
175
+
|166|[What are differences between mutations and actions?](#what-are-differences-between-mutations-and-actions)|
163
176
164
177
1.### What is VueJS?
165
178
**Vue.js** is an open-source, progressive Javascript framework for building user interfaces that aim to be incrementally adoptable. The core library of VueJS is focused on the `view layer` only, and is easy to pick up and integrate with other libraries or existing projects.
@@ -2819,4 +2832,191 @@ List of 300 VueJS Interview Questions
2819
2832
}
2820
2833
```
2821
2834
153. ### Do you need to replace entire local state with vuex?
2822
-
No, if a piece of state strictly belongs to a single component, it could be just fine leaving it as local state. i.e, Eventhough vuex used in the application, it doesn't mean that you need to keep all the local state in vuex store. Other the code becomes more verbose and indirect although it makes your state mutations more explicit and debuggable.
2835
+
No, if a piece of state strictly belongs to a single component, it could be just fine leaving it as local state. i.e, Eventhough vuex used in the application, it doesn't mean that you need to keep all the local state in vuex store. Other the code becomes more verbose and indirect although it makes your state mutations more explicit and debuggable.
2836
+
154. ### What are vuex getters??
2837
+
Vuex getters acts as computed properties for stores to compute derived state based on store state. Similar to computed properties, a getter's result is cached based on its dependencies, and will only re-evaluate when some of its dependencies have changed.
2838
+
Let's take a todo example which as completedTodos getter to find all completed todos,
The mapGetters is a helper that simply maps store getters to local computed properties. For example, the usage of getters for todo app would be as below,
2886
+
```javascript
2887
+
import { mapGetters } from'vuex'
2888
+
2889
+
exportdefault {
2890
+
computed: {
2891
+
// mix the getters into computed with object spread operator
2892
+
...mapGetters([
2893
+
'completedTodos',
2894
+
'todosCount',
2895
+
// ...
2896
+
])
2897
+
}
2898
+
}
2899
+
```
2900
+
158. ### What are mutations?
2901
+
Vuex mutations are similar to any events with a string `type` and a `handler`. The handler function is where we perform actual state modifications, and it will receive the state as the first argument.
2902
+
For example, the counter example with increment mutation would be as below,
2903
+
```javascript
2904
+
conststore=newVuex.Store({
2905
+
state: {
2906
+
count:0
2907
+
},
2908
+
mutations: {
2909
+
increment (state) {
2910
+
// mutate state
2911
+
state.count++
2912
+
}
2913
+
}
2914
+
})
2915
+
```
2916
+
You can't directly invoke mutation instead you need to call `store.commit` with its type. The above mutation would be triggered as folows
2917
+
```javascript
2918
+
store.commit('increment')
2919
+
```
2920
+
159. ### How do you commit with payload?
2921
+
You can also pass **payload** for the mutation as an additional argument to `store.commit`. For example, the counter mutation with payload object would be as below,
2922
+
```javascript
2923
+
mutations: {
2924
+
increment (state, payload) {
2925
+
state.count+=payload.increment
2926
+
}
2927
+
}
2928
+
```
2929
+
And then you can trigger increment commit
2930
+
```javascript
2931
+
store.commit('increment', {
2932
+
increment:20
2933
+
})
2934
+
```
2935
+
**Note:** You can also pass primitives as payload.
2936
+
160. ### What is object style commit?
2937
+
You can also commit a mutation is by directly using an object that has a **type** property.
2938
+
```javascript
2939
+
store.commit({
2940
+
type:'increment',
2941
+
value:20
2942
+
})
2943
+
```
2944
+
Now the entire object will be passed as the payload to mutation handlers(i.e, without any changes to handler signature).
2945
+
```javascript
2946
+
mutations: {
2947
+
increment (state, payload) {
2948
+
state.count+=payload.value
2949
+
}
2950
+
}
2951
+
```
2952
+
161. ### What are the caveats with vuex mutations?
2953
+
Since a Vuex store's state is made reactive by Vue, the same reactivity caveats of vue will apply to vuex mutations. These are the rules should be followed for vuex mutations,
2954
+
1. It is recommended to initialize store's initial state with all desired fields upfront
2955
+
2. Add new properties to state Object either by set method or object spread syntax
You need to remember that mutation handler functions must be synchronous. This is why because any state mutation performed in the callback is essentially un-trackable. It is going to be problematic when the devtool will need to capture a "before" and "after" snapshots of the state during the mutations.
2965
+
```javascript
2966
+
mutations: {
2967
+
someMutation (state) {
2968
+
api.callAsyncMethod(() => {
2969
+
state.count++
2970
+
})
2971
+
}
2972
+
}
2973
+
```
2974
+
163. ### How do you perform mutations in components?
2975
+
You can commit mutations in components with either **this.$store.commit('mutation name')** or mapMutations helper to map component methods to **store.commit** calls.
2976
+
For example, the usage of mapMutations helper on counter example would be as below,
2977
+
```javascript
2978
+
import { mapMutations } from'vuex'
2979
+
2980
+
exportdefault {
2981
+
methods: {
2982
+
...mapMutations([
2983
+
'increment', // map `this.increment()` to `this.$store.commit('increment')`
2984
+
2985
+
// `mapMutations` also supports payloads:
2986
+
'incrementBy'// map `this.incrementBy(amount)` to `this.$store.commit('incrementBy', amount)`
2987
+
]),
2988
+
...mapMutations({
2989
+
add:'increment'// map `this.add()` to `this.$store.commit('increment')`
2990
+
})
2991
+
}
2992
+
}
2993
+
```
2994
+
164. ### Is it mandatory to use constants for mutation types?
2995
+
No, it is not mandatory. But you might observed that State management implementations such Flux and Redux use constants for mutation types. This convention is just a preference and useful to take advantage of tooling like linters, and putting all constants in a single file allows your collaborators to get an at-a-glance view of what mutations are possible in the entire application.
2996
+
For example, the mutations can be declared as below,
2997
+
```javascript
2998
+
// mutation-types.js
2999
+
exportconstSOME_MUTATION='SOME_MUTATION'
3000
+
```
3001
+
And you can configure them in store as follows,
3002
+
```javascript
3003
+
// store.js
3004
+
importVuexfrom'vuex'
3005
+
import { SOME_MUTATION } from'./mutation-types'
3006
+
3007
+
conststore=newVuex.Store({
3008
+
state: { ... },
3009
+
mutations: {
3010
+
// ES2015 computed property name feature to use a constant as the function name
3011
+
[SOME_MUTATION] (state) {
3012
+
// mutate state
3013
+
}
3014
+
}
3015
+
})
3016
+
```
3017
+
165. ### How do you perform asynchronous operations?
3018
+
In Vuex, mutations are synchronous transactions. But if you want to handle asynchronous operations then you should use **actions**.
3019
+
166. ### What are differences between mutations and actions?
3020
+
Actions are similar to mutations, but there are two main differences,
3021
+
1. Mutations perform mutations on the state, actions commit mutations.
3022
+
2. Actions can contain arbitrary asynchronous operations unlike mutations.
0 commit comments