-
Notifications
You must be signed in to change notification settings - Fork 124
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
withContext and withMultiContexts #464
base: main
Are you sure you want to change the base?
Conversation
|
Possibly less essential for |
a utility function to wrap children in context without having to use JSX. see solidjs-community/solid-primitives#464
) { | ||
let result: JSX.Element | JSX.Element[]; | ||
|
||
const fn = (index: number) => { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Wouldn't it be simpler to use values.reduce((children, [context, value]), children) => ...)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Something like
(values as [Context<any>, any]).reduce((acc, [context, value], index) => {
return () =>
context.Provider({
value,
children: () => {
if (index === 0) result = acc();
else acc();
},
});
}, children)()
?
I don't use reduce
myself that much, so this code is a bit less readable to me personally, but that's mb more of a skill issue on my part.
|
||
fn(0); | ||
|
||
return () => result; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The accessor is redundant. We can also return result
directly.
}) as any as JSX.Element, | ||
}); | ||
|
||
return () => result; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The accessor is redundant. We can also return result
directly.
How would this function handle a custom context provider (in case the context is private)? const MyContext = createContext</*...*/>();
export const MyContextProvider: ParentComponent = (props) => { /* ... */ } I think you should add support for generic |
Using context with
jsx-tokenizer
can be quite painful. To remedy this I have been using a utility-functionwithContext
in my side-project solid-canvas (see).I modified this function into
withContext
andwithMultiContexts
to be more in-line with othersolid-primitives
likeMultiProvider
.API
withContext
withMultiContexts
the API mirrors
MultiProvider
(for now I have kept it simple and only[Context<T>, T]
is allowed), but the implementation is a bit different, since withjsx-tokenizer
we have to be careful not to resolve the tokens (see) this would cause a warning and prevent the token to be passed on.Its usage is not limited to
jsx-tokenizer
, it could be used with 'regular' solid as well.Demos: