You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
refactor(ui): simplify ConfigProvider, improve useControllableState types and defaultValue fallback (#14409)
Our `ConfigProvider` has a `useEffect` that updates its initial state if
the config from props changes (e.g. due to HMR running).
Turns out @jacobsfletch added this awesome hook called
`useControllableState` that already does exactly this: manage an
internal state and react to prop changes while ensuring it does not run
unnecessarily on mount.
---
**Improvements to `useControllableState`:**
- **Uses `useControllableState` in `ConfigProvider`** - reduces
duplicative state management code
- **Simplifies implementation** - removes redundant `useCallback`
wrapper around `setLocalValue`
- **Fixes fallback behavior** - `defaultValue` now consistently returns
when value is `null` or `undefined`, not just on initial render.
Previously would fail to fallback if value became null/undefined after
mounting (e.g. if useEffect ran).
- **Improves type safety** - `defaultValue` is a separate generic that
properly types the return value
**Type example:**
```typescript
const propValue: string | undefined = 'test' as string | undefined
// Without defaultValue - return type for value is string | undefined
const [value, setValue] = useControllableState(propValue)
// With defaultValue - return type for value is string. Before this PR, it would be string | undefined
const [value, setValue] = useControllableState(propValue, 'fallback')
```
0 commit comments