Skip to content

Commit

Permalink
feat: create useState composable
Browse files Browse the repository at this point in the history
  • Loading branch information
lauramargar committed Feb 1, 2024
1 parent b4e42b9 commit 0f86bf2
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 0 deletions.
1 change: 1 addition & 0 deletions packages/x-components/src/composables/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,4 @@ export * from './use-$x';
export * from './use-register-x-module';
export * from './use-on-display';
export * from './use-store';
export * from './use-state';
22 changes: 22 additions & 0 deletions packages/x-components/src/composables/use-state.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
import { computed } from 'vue';
import { ExtractState, XModuleName } from '../x-modules/x-modules.types';
import { useStore } from './use-store';

/**
* Generates a computed property which returns the selected state.
*
* @param module - The {@link XModuleName} of the getter.
* @param path - The state path.
* @returns The state properties of the module.
* @public
*/
export function useState<Module extends XModuleName, Path extends keyof ExtractState<Module>>(
module: Module,
path: Path
): ExtractState<Module>[Path] {
const $store = useStore();
const state = computed((): ExtractState<Module>[Path] => {
return $store.state.x[module][path];
});
return state.value;
}

0 comments on commit 0f86bf2

Please sign in to comment.