The simplest solution to lots of state management problems is to use a composable to create a shareable data store.
This pattern has a few parts:
Here's a simple example:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35
import { reactive, toRefs, readonly } from 'vue';import { themes } from './utils';// 1. Create global state in module scope, shared every// time we use this composableconst state = reactive({darkMode: false,sidebarCollapsed: false,// 2. This theme value is kept private to this composabletheme: 'nord',});export default () => {// 2. Expose only some of the state// Using toRefs allows us to share individual valuesconst { darkMode, sidebarCollapsed } = toRefs(state);// 3. Modify our underlying stateconst changeTheme = (newTheme) => {if (themes.includes(newTheme)) {// Only update if it's a valid themestate.theme = newTheme;}}return {// 2. Only return some of the statedarkMode,sidebarCollapsed,// 2. Only expose a readonly version of statetheme: readonly(state.theme),// 3. We return a method to modify underlying statechangeTheme,}}