2021-09-30
2512
#vue
Michael Gallagher
1742
Sep 30, 2021 ⋅ 8 min read

Vuex showdown: Mutations vs. actions

Michael Gallagher Living in the vibrant city of Buenos Aires, developing project management software with Teamwork. Modern JS is my passion; Vue, my tool of choice.

Recent posts:

Implementing Infinite Scroll In Next Js With Server Actions

Implementing infinite scroll in Next.js with Server Actions

Infinite scrolling in Next.js no longer requires external libraries — Server Actions let us fetch initial data directly on the server.

Rahul Chhodde
Apr 19, 2024 ⋅ 10 min read
Integrating Django Templates With React For Dynamic Webpages

Integrating Django templates with React for dynamic webpages

Create a dynamic demo blog site using Django and React to demonstrate Django’s server-side functionalities and React’s interactive UI.

Kayode Adeniyi
Apr 18, 2024 ⋅ 7 min read
Using Aoi Js To Build A Bot For Discord

Using aoi.js to build a bot on Discord

Explore how the aoi.js library makes it easy to create Discord bots with useful functionalities for frontend applications.

Rahul Padalkar
Apr 17, 2024 ⋅ 9 min read
Web Components Adoption Guide: Overview, Examples, And Alternatives

Web Components adoption guide: Overview, examples, and alternatives

Evaluate Web Components, a set of standards that allow you to create custom HTML tags for more reusable, manageable code.

Elijah Asaolu
Apr 16, 2024 ⋅ 11 min read
View all posts

2 Replies to "Vuex showdown: Mutations vs. actions"

  1. Hi Michael, thanks for this nice article! Vuex is a great tool but I think we are lacking some guidelines to follow to get it right.

    I often read that mutations should be one-liners , or at least remain really simple… Why? The doc describes a mutation as a transaction, this term makes me think that we could write as much code as needed in a single mutation to enforce state coherency.

    Let’s take a simple example: an image with its width and height stored in the state, and a boolean to keep aspect ratio when changing the width or height. Would you advise to write the keep aspect ratio logic in the ´setWidth’ and ´setHeight’ mutations, or move it into the actions?

    Thanks!

  2. Hey Jonathan,

    Glad you like the article, I’ve tried my best not to be overly opinionated about it. I normally use actions, even if it is just wrapping a mutation. The question for me in your example is whether there are 1 or 2 actions/mutations. You could have the 2 you mention, or maybe `setDimensions` which would set both? The reason I say it is that, to me, the width and height will probably always be set together, considering them as separate transactions for either mutations or actions, doesn’t give any additional benefit to traceability, readability or reusability. And this would cover your aspect ratio point also.

    Cheers
    Mike

Leave a Reply