Skip to content

Add createContextMethods option - #55

Open
SeokminHong wants to merge 5 commits into
ArnaudBarre:mainfrom
SeokminHong:main
Open

Add createContextMethods option#55
SeokminHong wants to merge 5 commits into
ArnaudBarre:mainfrom
SeokminHong:main

Conversation

@SeokminHong

@SeokminHong SeokminHong commented Oct 25, 2024

Copy link
Copy Markdown

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

@ArnaudBarre ArnaudBarre left a comment

Copy link
Copy Markdown
Owner

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?

Comment thread src/only-export-components.ts Outdated
Comment thread src/only-export-components.test.ts Outdated
Comment thread src/only-export-components.test.ts Outdated
Comment thread src/only-export-components.test.ts Outdated
@SeokminHong

Copy link
Copy Markdown
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é <arnaud.barre72@gmail.com>
Co-authored-by: Arnaud Barré <arnaud.barre72@gmail.com>
@ArnaudBarre

Copy link
Copy Markdown
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?

@ArnaudBarre ArnaudBarre added question Further information is requested and removed question Further information is requested labels Apr 21, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

question Further information is requested

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants