Skip to content

Add ConsumeContext#756

Open
FabianHummel wants to merge 6 commits into
solidjs-community:mainfrom
FabianHummel:context-consume
Open

Add ConsumeContext#756
FabianHummel wants to merge 6 commits into
solidjs-community:mainfrom
FabianHummel:context-consume

Conversation

@FabianHummel
Copy link
Copy Markdown

Add a function to directly consume contexts within JSX. Inspired by React's Context.Consumer component, but nicely integrated with this primitive's createContextProvider.

Tests have been added and source code + README.md is documented

@changeset-bot
Copy link
Copy Markdown

changeset-bot Bot commented Feb 15, 2025

🦋 Changeset detected

Latest commit: e71e4ad

The changes in this PR will be included in the next version bump.

This PR includes changesets to release 1 package
Name Type
@solid-primitives/context Minor

Not sure what this means? Click here to learn what changesets are.

Click here if you're a maintainer who wants to add another changeset to this PR

@thetarnav
Copy link
Copy Markdown
Member

this is a cool idea, but I don't like how you need a bound function (without parameters) to use it. When the default solid api for using the context is useContext(Ctx). At least it should be added to docs. To pass it as () => useContext(Ctx) or useContext.bind(null, Ctx)
Also you can easily do this in solid and it will have the same effect

<Provider>
{untrack(() => {
    const value = useContext(Ctx)
    return <>{value}</>
})}
</Provider>

@FabianHummel
Copy link
Copy Markdown
Author

If you mean the useFn parameter, yes, it would be better to change it to the context directly. Maybe allow for either of one, because I really like simply passing the use function that is returned from createContextProvider, especially because createContextProvider does not return the raw solidJS context:

<ConsumeContext context={counterContext}>
  {(ctx) => <>...</>}
</ConsumeContext>

// or

<ConsumeContext useFn={useCounter}>
  {(ctx) => <>...</>}
</ConsumeContext>

And for the easy alternative: <ConsumeContext> solely serves as syntactic sugar, just like <Show>, <For>, etc...

@FabianHummel
Copy link
Copy Markdown
Author

Implemented the aforementioned ideas and added tests + docs

@thetarnav
Copy link
Copy Markdown
Member

what about

<ConsumeContext use={useMyContext}>
<ConsumeContext use={() => useContext(MyContext)}>
<ConsumeContext use={MyContext}>

by simply checking the typeof props.use?

Also it may be useful to mention in the docs that the prop will not be reactive.

@FabianHummel
Copy link
Copy Markdown
Author

Oh, I totally forgot about this option, I will implement it later that day, thanks!

@davedbase
Copy link
Copy Markdown
Member

Hey there! It's been quite a while since this was first introduced. We're working on Solid 2.0 migration right now and would like to consider this for the porting. Do you have time to finish off the current work for 1.0 or possibly support 2.0 directly?

@FabianHummel
Copy link
Copy Markdown
Author

Hi, I am pleased to see this PR regaining some attention, it kind of went under my radar to be honest 😅

It's cool to see progress being made on SolidJS and I would still love to contribute to its ecosystem. I can invest some time updating this PR to Solid 2.0 if it's fine for you. Are there things I need to look out for like changing the branch, or can I continue work as usual?

@davedbase
Copy link
Copy Markdown
Member

I'd recommend merging in main to ensure you're on latest and you can continue working as you'd like. Can land 1.0, publish and get a 2.0 or going. :)

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.

3 participants