Skip to content

Commit

Permalink
prevent unnecessary re-renders when the state is already correct
Browse files Browse the repository at this point in the history
  • Loading branch information
RobinMalfait committed Feb 18, 2021
1 parent fd3f4ac commit 854cc83
Showing 1 changed file with 12 additions and 7 deletions.
19 changes: 12 additions & 7 deletions packages/@headlessui-react/src/components/menu/menu.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -76,12 +76,14 @@ let reducers: {
action: Extract<Actions, { type: P }>
) => StateDefinition
} = {
[ActionTypes.CloseMenu]: state => ({
...state,
activeItemIndex: null,
menuState: MenuStates.Closed,
}),
[ActionTypes.OpenMenu]: state => ({ ...state, menuState: MenuStates.Open }),
[ActionTypes.CloseMenu](state) {
if (state.menuState === MenuStates.Closed) return state
return { ...state, activeItemIndex: null, menuState: MenuStates.Closed }
},
[ActionTypes.OpenMenu](state) {
if (state.menuState === MenuStates.Open) return state
return { ...state, menuState: MenuStates.Open }
},
[ActionTypes.GoToItem]: (state, action) => {
let activeItemIndex = calculateActiveIndex(action, {
resolveItems: () => state.items,
Expand All @@ -103,7 +105,10 @@ let reducers: {
if (match === -1 || match === state.activeItemIndex) return { ...state, searchQuery }
return { ...state, searchQuery, activeItemIndex: match }
},
[ActionTypes.ClearSearch]: state => ({ ...state, searchQuery: '' }),
[ActionTypes.ClearSearch](state) {
if (state.searchQuery === '') return state
return { ...state, searchQuery: '' }
},
[ActionTypes.RegisterItem]: (state, action) => ({
...state,
items: [...state.items, { id: action.id, dataRef: action.dataRef }],
Expand Down

0 comments on commit 854cc83

Please sign in to comment.