UNPKG

vue-hooks-plus

Version:
18 lines (17 loc) 514 B
import { ref, readonly, unref } from "vue"; import merge from "lodash-es/merge"; function useSetState(initialState) { const getInitialState = () => unref(initialState); const state = ref(getInitialState()); const setMergeState = (patch, cover = false) => { const newState = unref(patch); if (cover) state.value = newState; else state.value = newState ? merge(state.value, newState) : state.value; }; return [readonly(state), setMergeState]; } export { useSetState as default };