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

Cannot create a component with generic props with reatomComponent #850

Open
MichailShcherbakov opened this issue May 24, 2024 · 4 comments
Labels
good first issue Good for newcomers

Comments

@MichailShcherbakov
Copy link
Contributor

What happened?

The reatomComponent has next props type:

props: T & { ctx: CtxRender; }

I need to create a component with generic props as shown below:

interface GenericComponentProps<T> {
  items: T
}

const GenericComponent = reatomComponent(<T,>({ ctx, items }: GenericeComponentProps<T>) => {}, 'GenericeComponent');

But obviously ctx is not there.

I tried to import CtxRender and put it in the props but @reatom/npm-react doesn't export it.

So it would be great if you add something like this (inspired by React.PropsWithChildren)

type PropsWithCtx<T = unknown> = T & { ctx: CtxRender };

or at least you should export CtxRender in @reatom/npm-react

Part

Package @reatom/npm-react

Version

3.8.6

What browsers are you seeing the problem on?

Microsoft Edge

Relevant log output

No response

@MichailShcherbakov MichailShcherbakov added the bug Something isn't working label May 24, 2024
@artalar
Copy link
Owner

artalar commented May 25, 2024

It is imposible in TypeScript to to accept a generic and return it via other generic function. Unfortunatly you shuold cast the component type, like this: const GenericComponent = reatomComponent<BaseProps>(render) as <T extends BaseProps>(prop: T) => JSX.Element

@artalar artalar closed this as completed May 25, 2024
@artalar artalar removed the bug Something isn't working label May 25, 2024
@MichailShcherbakov
Copy link
Contributor Author

MichailShcherbakov commented May 26, 2024

@artalar Maybe I'm wrong, but I think it's possible to avoid component type casting. How about that?

Example

@artalar
Copy link
Owner

artalar commented Oct 8, 2024

@MichailShcherbakov nice! We need to add this to our sources!

@artalar artalar reopened this Oct 8, 2024
@artalar artalar added the good first issue Good for newcomers label Oct 8, 2024
@MichailShcherbakov
Copy link
Contributor Author

@artalar I'm glad to hear that. I've created a PR with an example how it could be implemented. I've changed the props type inferring to provide more clear props type of components.

In the PR example it will be:

import { reatomComponent, PropsWithCtx } from "@reatom/npm-react";

interface ListProps<T> {
  items: T[];
  onItemClick: (item: T) => void;
}

// const List: <T>(props: ListProps<T>) => JSX.Element
const List = reatomComponent(<T,>({ ctx, items, onItemClick }: PropsWithCtx<ListProps<T>>) => {
  return null;
}, 'List');

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
good first issue Good for newcomers
Projects
None yet
Development

No branches or pull requests

2 participants