📝

Turbo Tutorial on Using Pinia for State Management

Jul 14, 2024

Turbo Tutorial on Using Pinia for State Management

Introduction

  • Presenter: Tim Bennett
  • Topic: Using Pinia, a state management tool for Vue.js and Nuxt.js.
  • Scope: Installation, tips, basic features, and practical usage.

Installation

  1. Nuxt 3 Clean Install: Use a clean Nuxt 3 installation and add the Nuxt Pinia module.
  2. Auto Import Property: Enables automatic import of defineStore and other necessary functions.
  3. Stores Directory: Add the stores directory to Nuxt imports for automatic integration.

Optimization Tips

  • Hot Module Reloading (HMR): Add specific lines in the store for HMR, enabling UI updates without refreshing the page.

Basic Store Setup

  • Store Name: useFriendsStore (TV Show Friends theme).
  • State: Contains the core data structure, here it includes characters from Friends.
  • Actions: Methods to perform actions that update the state.
  • Getters: Methods to retrieve specific data from the state.

Practical Example

  • Component: Created lines.vue to interact with the store.
  • Using Store: Use useFriendsStore to get the state in the component.
  • Stores to Ref: Helper function for reactive state updates.

Actions

  • Function: addCharacter(name, line) to add a new character with a line of dialogue.
  • Example: Adding Joey to the state via a button click.

Getters

  • Function: getLineByName(name) to retrieve a character's line by their name.
  • Example: Retrieving Rachel and Ross's lines specifically using the getter.

Conclusion

  • Interactive: Encouraged to ask questions in comments.
  • Final Note: Happy coding!