Skip to content

Commit

Permalink
feat: allow useMemo hook in server components
Browse files Browse the repository at this point in the history
  • Loading branch information
Dwlad90 committed Jul 24, 2024
1 parent d3c3cf6 commit fcc1430
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 2 deletions.
5 changes: 5 additions & 0 deletions .changeset/loud-terms-confess.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"eslint-plugin-react-server-components": minor
---

allow useMemo hook in RSC
30 changes: 30 additions & 0 deletions src/rules/use-client.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -171,6 +171,36 @@ export function Foo() {
}`,
options: [{ allowedServerHooks: ["useTranslations"] }],
},
{
code: `import React from 'react';
export function Foo({id}) {
const t = React.useState(id);
return <button id={t} />;
}`,
options: [{ allowedServerHooks: ["useState"] }],
},
{
code: `import * as React from 'react';
export function Foo({id}) {
const t = React.useState(id);
return <button id={t} />;
}`,
options: [{ allowedServerHooks: ["useState"] }],
},
{
code: `import {useMemo} from 'react';
const Button = ({id}) => {
const memoizedId = useMemo(() => id, [id]);
return <div id={memoizedId} />;
}`,
},
{
code: `import React from 'react';
const Button = ({id}) => {
const memoizedId = React.useMemo(() => id, [id]);
return <div id={memoizedId} />;
}`,
},
],
invalid: [
{
Expand Down
3 changes: 1 addition & 2 deletions src/rules/use-client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -81,9 +81,8 @@ const create = Components.detect(
function isClientOnlyHook(name: string) {
return (
// `useId` is the only hook that's allowed in server components
name !== "useId" &&
!(options.allowedServerHooks || []).includes(name) &&
/^use[A-Z]/.test(name)
/^use(?!(Id|Memo)$)[A-Z]/.test(name)
);
}

Expand Down

0 comments on commit fcc1430

Please sign in to comment.