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

Add createContextMethods option #55

Open
wants to merge 5 commits into
base: main
Choose a base branch
from

Conversation

SeokminHong
Copy link

@SeokminHong SeokminHong commented Oct 25, 2024

This allows users to specify custom context-creating methods, in addition to the default createContext from React.

{
  "rules": {
    "react-refresh/only-export-components": ["error", {
      "createContextMethods": ["createMyContext"]
    }]
  }
}
import { createMyContext } from './utils';

export const MyComponent = () => <div />;
// error
export const MyContext = createMyContext();

Related: #54

Copy link
Owner

@ArnaudBarre ArnaudBarre left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm open to this option! What is your reason to wrap the createContext method?

src/only-export-components.ts Outdated Show resolved Hide resolved
src/only-export-components.test.ts Outdated Show resolved Hide resolved
src/only-export-components.test.ts Outdated Show resolved Hide resolved
src/only-export-components.test.ts Outdated Show resolved Hide resolved
@SeokminHong
Copy link
Author

I'm open to this option! What is your reason to wrap the createContext method?

Sorry for late response 🙏

One use case for this is Radix UI, which uses a custom createContext method to prevent useContext from being called outside its intended scope.

https://github.com/radix-ui/primitives/blob/74b182b401c8ca0fa5b66a5a9a47f507bb3d5adc/packages/react/context/src/createContext.tsx

My team also uses similar methods, so I'd like to allow contexts created using these approaches.

SeokminHong and others added 3 commits November 2, 2024 18:41
Co-authored-by: Arnaud Barré <[email protected]>
Co-authored-by: Arnaud Barré <[email protected]>
@ArnaudBarre
Copy link
Owner

Thanks for the update.

I'm still unsure on how this pattern from Radix impact source code in project that use it. Do you have an example? (I've not used Radix yet).

So you also have files that have factories that create the useContext and Provider together? I'm a bit afraid that this leads to bad HMR experience. Can you provide a small example like what a simple UserContext would look like?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants