Skip to content

Commit 41f32e1

Browse files
committed
perf(navbar): memoize context provider value
1 parent 7dacfaf commit 41f32e1

File tree

1 file changed

+11
-6
lines changed

1 file changed

+11
-6
lines changed

src/Navbar.js

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import classNames from 'classnames';
2-
import React from 'react';
2+
import React, { useMemo } from 'react';
33
import PropTypes from 'prop-types';
44

55
import { useUncontrolled } from 'uncontrollable';
@@ -152,10 +152,6 @@ const Navbar = React.forwardRef((props, ref) => {
152152
}
153153
};
154154

155-
const handleToggle = () => {
156-
onToggle(!expanded);
157-
};
158-
159155
// will result in some false positives but that seems better
160156
// than false negatives. strict `undefined` check allows explicit
161157
// "nulling" of the role if the user really doesn't want one
@@ -165,8 +161,17 @@ const Navbar = React.forwardRef((props, ref) => {
165161
let expandClass = `${bsPrefix}-expand`;
166162
if (typeof expand === 'string') expandClass = `${expandClass}-${expand}`;
167163

164+
const navbarContext = useMemo(
165+
() => ({
166+
handleToggle: () => onToggle(!expanded),
167+
bsPrefix,
168+
expanded,
169+
}),
170+
[bsPrefix, expanded, onToggle],
171+
);
172+
168173
return (
169-
<NavbarContext.Provider value={{ handleToggle, bsPrefix, expanded }}>
174+
<NavbarContext.Provider value={navbarContext}>
170175
<SelectableContext.Provider value={handleCollapse}>
171176
<Component
172177
ref={ref}

0 commit comments

Comments
 (0)