-
Notifications
You must be signed in to change notification settings - Fork 137
JSX Tokenizer: add tokens which can be parsed using children
#399
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
Comments
+1 the whole |
little experiment with simply casting: const token = (args[0] ? args[0](props) : props) as unknown as TokenElement<T>;
token[$TOKENIZER] = symbol;
return token as unknown as JSX.Element; then we can only accept objects as return-values, but the abstraction looks a bit cleaner: const Token = createToken(() => ({ id: 'string' }));
const Parent = (props: {children: JSX.Element)) => {
const token = props.children;
token.id // 'string'
// instead of
token.data.id // 'string'
}) We get the following warning if a token accidentally gets added to the DOM-tree in dev-mode, so don't lose error-handling completely: ![]() Context becomes a lot easier too: const childs = <Context.Provider value={...}>{props.children}</Context.Provider> just works. This is 💯 the way forward. |
It's more or less what I want to do |
Yes, We wouldn't really need to |
I haven't really touched this library but hopefully the idea gets across. I'm not sure where this is at the moment, but I don't believe using an object solves the issues outlined. Instead, it may make errors more difficult to spot. Consider the following: <Resolver>
<Context.Provider>
<Token>
<Context.Consumer />
</Token>
</Context.Provider>
</Resolver> Using an object, the consumer would fail to read the context since it would be executed outside the provider. Similarly, with In other words, consider keeping the current implementation the primary one, and adding a |
Hi @otonashixav Not sure if I completely follow. I have been using this pattern with context without any problems. Not too familiar with From my experiments in this playground I was only able to reproduce the context-bug you described by passing the children as a getter to the resolver, and that feels pretty intuitive to me. You mentioned in discord that adding an unused argument would prevent it from being resolved with Having objects as the containers is just less finnicky then functions imo. |
Yes, I mentioned in my playground example that the unused parameter would not work properly with Show. In any case it was an example of what could work, not what to actually use -- I suggested this library because I thought it was the most foolproof. |
Yes, I know, It was just to indicate that context worked as expected.
I don't think I follow here. What would be the usecase? Sounds like behavior that would break how you would expect components to behave regularly. I don't really understand how using functions as data-containers would help here.
|
I think this creates other issues with props spread, see: https://stackblitz.com/edit/solid-jsx-tokenizer-spread?file=src%2Flib%2Findex.tsx |
Describe The Problem To Be Solved
Currently, the component returned by
createToken
will return a function element. This way we can display a friendly warning in the console if something tries to render the token, instead of an error from trying to insert a non-JSX.Element
type.The downside of that approach is that the token elements cannot be passed to a context provider, transition-group, or anything that tries to resolve the children using the
children
helper, even if it doesn't necessarily render anything yet.So we lose a bit of functionality, and composition value, for a nicer misuse warning.
Suggest A Solution
I see this being implemented in two ways.
Either the
createToken
component will be returning an object, instead of a function if thefallbackFn
param is not provided.Or there could be
createToken
andcreateRenderableToken
as separate functions.The text was updated successfully, but these errors were encountered: