Skip to content

Commit faaac52

Browse files
committed
Add vuex actions questions and answers
1 parent f1bfcd4 commit faaac52

File tree

1 file changed

+22
-1
lines changed

1 file changed

+22
-1
lines changed

README.md

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -173,6 +173,7 @@ List of 300 VueJS Interview Questions
173173
|164| [Is it mandatory to use constants for mutation types?](#is-it-mandatory-to-use-constants-for-mutation-types)|
174174
|165| [How do you perform asynchronous operations?](#how-do-you-perform-asynchronous-operations)|
175175
|166| [What are differences between mutations and actions?](#what-are-differences-between-mutations-and-actions)|
176+
|167| [Give an example usage of actions?](#give-an-example-usage-of-actions?)|
176177

177178
1. ### What is VueJS?
178179
**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.
@@ -3019,4 +3020,24 @@ List of 300 VueJS Interview Questions
30193020
166. ### What are differences between mutations and actions?
30203021
Actions are similar to mutations, but there are two main differences,
30213022
1. Mutations perform mutations on the state, actions commit mutations.
3022-
2. Actions can contain arbitrary asynchronous operations unlike mutations.
3023+
2. Actions can contain arbitrary asynchronous operations unlike mutations.
3024+
167. ### Give an example usage of actions?
3025+
Vuex provides actions property similar mutations property in order to define action handlers. These action handlers receive context object as an argument which has same properties and methods of store instance.
3026+
Let's see counter example to demonstrate increment action which commits respective mutation,
3027+
```javascript
3028+
const store = new Vuex.Store({
3029+
state: {
3030+
count: 0
3031+
},
3032+
mutations: {
3033+
increment (state) {
3034+
state.count++
3035+
}
3036+
},
3037+
actions: {
3038+
increment (context) {
3039+
context.commit('increment')
3040+
}
3041+
}
3042+
})
3043+
```

0 commit comments

Comments
 (0)