Performance
This document describes good/bad practices for performance. If you have any concerns/questions regarding performance, feel free to reach out to us by reporting a GitHub issue.
Fluent V7 components support styles
prop for you to customize styles. It can impact performance negatively if it's constantly mutating on every render. This is because if styles
is changed by reference, styles need to be recalculated with a cost.
Styles is an object.
const checkboxStyles = { root: { background: 'red' } };
const App = () => {
return <Checkbox styles={checkboxStyles} />;
};
const App = () => {
return (
<Checkbox styles={{ root: { background: 'red' }} />
);
};
Styles is a function.
const checkboxStyles = ({ checked }) => ({ root: { background: checked ? 'red' : 'blue' } });
const App = () => {
return <Checkbox styles={checkboxStyles} />;
};
const App = () => {
return (
<Checkbox styles={({ checked }) => ({ root: { background: checked ? 'red' : 'blue' } })} />
);
};
With memoization.
import { memoizeFunction } from '@fluentui/react/lib/Utilities';
const getCheckboxStyles = memoizeFunction(background => ({ root: { background } }));
const App = () => {
const [color, setColor] = React.useState('red');
const toggleColor = () => setColor(color === 'red' ? 'blue' : 'red');
return (
<>
<button onClick={toggleColor}>Toggle color</button>
<Checkbox styles={getCheckboxStyles(backgroundColor)} />
</>
);
};
New styles object will be passed every time getCheckboxStyles
is called :(
const getCheckboxStyles = background => ({ root: { background } });
const App = () => {
const [color, setColor] = React.useState('red');
const toggleColor = () => setColor(color === 'red' ? 'blue' : 'red');
return (
<>
<button onClick={toggleColor}>Toggle color</button>
<Checkbox styles={getCheckboxStyles(backgroundColor)} />
</>
);
};
Do not use React.useMemo
for memoizing styles creation, because it resets the cache every time component unmounts. It will help in case of re-renders, but not if the same component gets mounted multiple times.
const App = () => {
const [color, setColor] = React.useState('red');
const toggleColor = () => setColor(color === 'red' ? 'blue' : 'red');
const checkboxStyles = React.useMemo(() => ({ root: { background: color } }), [color]);
return (
<>
<button onClick={toggleColor}>Toggle color</button>
<Checkbox styles={checkboxStyles} />
</>
);
};
- FAQ - Fabric and Stardust to Fluent UI
-
@fluentui/react
Version 9 -
@fluentui/react
Version 8 - Contributing to the
7.0
branch - How to apply themes (version 7/8)
- Planning and development process (for work by the core team)
- Conducting meetings Style guide
- Keeping up with review requests
- RFC review process
- Setup (configuring your environment)
- Fluent UI React version 7/8
- CLA
- Overview
- Repo structure
- Development process
- Contributing to previous versions
- API Extractor
- Build command changes made in early 2020
- Component implementation guide
- Creating a component
- Implementation Best Practices
- Theming
- Documenting
- Styling (old approach)
- Overview
- Testing with Jest
- E2E testing (Cypress)
- Visual testing (Screener)
- Accessibility review checklist