UNPKG

sunmao-sdk

Version:

榫卯-开箱即用赋能-sdk

23 lines (21 loc) 618 B
import { useReducer } from "react"; export const useSet = initState => { const [state, setState] = useReducer((state, newState) => { let action = newState; if (typeof newState === "function") { action = action(state); } if (newState.action && newState.payload) { action = newState.payload; if (typeof action === "function") { action = action(state); } } const result = { ...state, ...action }; return result; }, initState); const setStateWithActionName = (state, actionName) => { setState(state); }; return [state, setStateWithActionName]; };