Skip to content

Commit 9920abc

Browse files
authored
fix(react-icons): fix react rules of hook violations (#874)
* chore(react-icons): add rules of hooks lint plugin * fix(react-icons): fix react rules of hook violations
1 parent e7217c3 commit 9920abc

File tree

5 files changed

+32
-2
lines changed

5 files changed

+32
-2
lines changed

package-lock.json

Lines changed: 22 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

packages/react-icons/eslint.config.mjs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
// @ts-check
22
import tseslint from 'typescript-eslint';
33
import griffelPlugin from '@griffel/eslint-plugin';
4+
import reactHooksPlugin from 'eslint-plugin-react-hooks';
45

56
export default tseslint.config(
67
{
@@ -26,10 +27,13 @@ export default tseslint.config(
2627
files: ['**/*.{ts,tsx}'],
2728
plugins: {
2829
'@griffel': griffelPlugin,
30+
'react-hooks': reactHooksPlugin,
2931
},
3032
rules: {
3133
'@griffel/no-shorthands': 'error',
3234
'@griffel/styles-file': 'error',
35+
'react-hooks/rules-of-hooks': 'error',
36+
'react-hooks/exhaustive-deps': 'warn',
3337
},
3438
},
3539
);

packages/react-icons/package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@
4040
"@types/react": "^17.0.2",
4141
"concurrently": "^9.2.0",
4242
"eslint": "^8.57.0",
43+
"eslint-plugin-react-hooks": "^5.2.0",
4344
"glob": "^7.2.0",
4445
"lodash": "^4.17.21",
4546
"mkdirp": "^1.0.4",

packages/react-icons/src/contexts/IconDirectionContext.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,4 +10,7 @@ const IconDirectionContextDefaultValue: IconDirectionContextValue = {};
1010

1111
export const IconDirectionContextProvider = IconDirectionContext.Provider;
1212

13-
export const useIconContext = () => React.useContext(IconDirectionContext) ? React.useContext(IconDirectionContext) : IconDirectionContextDefaultValue
13+
export const useIconContext = () => {
14+
const context = React.useContext(IconDirectionContext);
15+
return context ?? IconDirectionContextDefaultValue;
16+
}

packages/react-icons/src/utils/createFluentIcon.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,9 +17,9 @@ export type CreateFluentIconOptions = {
1717

1818

1919
export const createFluentIcon = (displayName: string, width: string, pathsOrSvg: string[] | string, options?: CreateFluentIconOptions): FluentIcon => {
20-
const styles = useRootStyles();
2120
const viewBoxWidth = width === "1em" ? "20" : width;
2221
const Icon = React.forwardRef((props: FluentIconsProps, ref: React.Ref<HTMLElement>) => {
22+
const styles = useRootStyles();
2323
const iconState = useIconState(props, { flipInRtl: options?.flipInRtl }); // HTML attributes/props for things like accessibility can be passed in, and will be expanded on the svg object at the start of the object
2424
const state = {
2525
...iconState,

0 commit comments

Comments
 (0)