UNPKG

@moxy/next-layout

Version:

Add persistent and nested layouts to your Next.js projects in a declarative way

21 lines (16 loc) 627 B
import { useState, useRef } from 'react'; const createSetObjectState = setState => newState => { const updater = typeof newState === 'function' ? newState : () => newState; return setState(state => ({ ...state, ...updater(state) })); }; const useObjectState = initialState => { const [state, setState] = useState(() => initialState !== null && initialState !== void 0 ? initialState : {}); const setObjectStateRef = useRef(); if (!setObjectStateRef.current) { setObjectStateRef.current = createSetObjectState(setState); } return [state, setObjectStateRef.current]; }; export default useObjectState;