Skip to content

Commit fcc1430

Browse files
committed
feat: allow useMemo hook in server components
1 parent d3c3cf6 commit fcc1430

File tree

3 files changed

+36
-2
lines changed

3 files changed

+36
-2
lines changed

.changeset/loud-terms-confess.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"eslint-plugin-react-server-components": minor
3+
---
4+
5+
allow useMemo hook in RSC

src/rules/use-client.test.ts

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -171,6 +171,36 @@ export function Foo() {
171171
}`,
172172
options: [{ allowedServerHooks: ["useTranslations"] }],
173173
},
174+
{
175+
code: `import React from 'react';
176+
export function Foo({id}) {
177+
const t = React.useState(id);
178+
return <button id={t} />;
179+
}`,
180+
options: [{ allowedServerHooks: ["useState"] }],
181+
},
182+
{
183+
code: `import * as React from 'react';
184+
export function Foo({id}) {
185+
const t = React.useState(id);
186+
return <button id={t} />;
187+
}`,
188+
options: [{ allowedServerHooks: ["useState"] }],
189+
},
190+
{
191+
code: `import {useMemo} from 'react';
192+
const Button = ({id}) => {
193+
const memoizedId = useMemo(() => id, [id]);
194+
return <div id={memoizedId} />;
195+
}`,
196+
},
197+
{
198+
code: `import React from 'react';
199+
const Button = ({id}) => {
200+
const memoizedId = React.useMemo(() => id, [id]);
201+
return <div id={memoizedId} />;
202+
}`,
203+
},
174204
],
175205
invalid: [
176206
{

src/rules/use-client.ts

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -81,9 +81,8 @@ const create = Components.detect(
8181
function isClientOnlyHook(name: string) {
8282
return (
8383
// `useId` is the only hook that's allowed in server components
84-
name !== "useId" &&
8584
!(options.allowedServerHooks || []).includes(name) &&
86-
/^use[A-Z]/.test(name)
85+
/^use(?!(Id|Memo)$)[A-Z]/.test(name)
8786
);
8887
}
8988

0 commit comments

Comments
 (0)