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

withContext and withMultiContexts #464

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

Conversation

bigmistqke
Copy link
Contributor

Using context with jsx-tokenizer can be quite painful. To remedy this I have been using a utility-function withContext in my side-project solid-canvas (see).

I modified this function into withContext and withMultiContexts to be more in-line with other solid-primitives like MultiProvider.

API

withContext

const NumberContext = createContext<number>
const children = withContext(
   () => props.children,
   NumberContext,
   1
)

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 with jsx-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.

const NumberContext = createContext<number>
const StringContext = createContext<string>
const children = withContext(
   () => props.children,
   [
     [NumberContext, 1],
     [StringContext, "string"]
   ]
)

Its usage is not limited to jsx-tokenizer, it could be used with 'regular' solid as well.

Demos:

@changeset-bot
Copy link

changeset-bot bot commented Jun 19, 2023

⚠️ No Changeset found

Latest commit: 3347f00

Merging this PR will not cause a version bump for any packages. If these changes should not result in a new version, you're good to go. If these changes should result in a version bump, you need to add a changeset.

This PR includes no changesets

When changesets are added to this PR, you'll see the packages that this PR includes changesets for and the associated semver types

Click here to learn what changesets are, and how to add one.

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

@bigmistqke
Copy link
Contributor Author

Possibly less essential for jsx-tokenizer if we would change its implementation as proposed here instead, but still could be handy I think.

bigmistqke added a commit to solidjs-community/solid-three that referenced this pull request Jul 19, 2023
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) => {
Copy link
Member

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) => ...)

Copy link
Contributor Author

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)()

?

playground

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;
Copy link
Contributor Author

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;
Copy link
Contributor Author

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.

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.

None yet

2 participants