Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow useMemo hook in server components #13

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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