* Set common package.json sideEffects: false * Configure SWC to modularize lodash imports * Import specific lodash functions instead of _ * Add an eslint rule to avoid full lodash import
		
			
				
	
	
		
			22 lines
		
	
	
		
			503 B
		
	
	
	
		
			TypeScript
		
	
	
	
	
	
			
		
		
	
	
			22 lines
		
	
	
		
			503 B
		
	
	
	
		
			TypeScript
		
	
	
	
	
	
| import { isEqual } from 'lodash'
 | |
| import { useMemo, useRef, useState } from 'react'
 | |
| 
 | |
| export const useStateCheckEquality = <T>(initialState: T) => {
 | |
|   const [state, setState] = useState(initialState)
 | |
| 
 | |
|   const stateRef = useRef(state)
 | |
|   stateRef.current = state
 | |
| 
 | |
|   const checkSetState = useMemo(
 | |
|     () => (newState: T) => {
 | |
|       const state = stateRef.current
 | |
|       if (!isEqual(state, newState)) {
 | |
|         setState(newState)
 | |
|       }
 | |
|     },
 | |
|     [stateRef]
 | |
|   )
 | |
| 
 | |
|   return [state, checkSetState] as const
 | |
| }
 |