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

[Suggestion] Extracting props of a component #63

Closed
ferdaber opened this issue Jan 11, 2019 · 6 comments
Closed

[Suggestion] Extracting props of a component #63

ferdaber opened this issue Jan 11, 2019 · 6 comments
Labels
wontfix This will not be worked on

Comments

@ferdaber
Copy link
Collaborator

Not sure where to put it, but I have these helper types in my team's codebase to extract the props of a component:

// helper type for all known valid JSX element constructors (class and function based)
type ElementConstructor<P> =
  | ((props: P) => React.ReactElement<any> | null)
  | (new (props: P) => React.Component<P, any, any>

// gets the internal props of a component
// used like Props<typeof MyComponent>
// or Props<'button'> for intrinsic HTML attributes
type Props<C> = C extends ElementConstructor<infer P>
  ? P
  : C extends keyof JSX.IntrinsicElements
  ? JSX.InstrinsicElements[C]
  : {}

// goes one step further and resolves with propTypes and defaultProps properties
type ApparentProps<C> = C extends ElementConstructor<infer P> ? JSX.LibraryManagedAttributes<C, P> : Props<C>

As far as how we use them, there's a lot of places where I want to reuse some slices of props because of prop drilling, and I can either export the props type as part of the module or I use this (either way works), the advantage of using this is that a refactor of the source of truth component will propagate to all consuming components.

I also use them to strongly type custom event handlers if they're not written at the call sites themselves (i.e. inlined with the JSX attribute):

// my-inner-component.tsx
export function MyInnerComponent(props: { onSomeEvent(event: ComplexEventObj, moreArgs: ComplexArgs): SomeWeirdReturnType }) { /* ... */ }

// my-consuming-component.tsx
export function MyConsumingComponent() {
  // event and moreArgs are contextually typed along with the return value
  const theHandler: Props<typeof MyInnerComponent>['onSomeEvent'] = (event, moreArgs) => {}
  return <MyInnerComponent onSomeEvent={theHandler} />
}
@swyxio
Copy link
Collaborator

swyxio commented Jan 11, 2019

ok this took me a while to understand but wow. super handy!!! i love it. i'm gonna break up the cheatsheet to a basic and an advanced section, give me a while

@swyxio
Copy link
Collaborator

swyxio commented Jan 11, 2019

done https://github.com/sw-yx/react-typescript-cheatsheet/blob/master/ADVANCED.md#extracting-prop-types-of-a-component

@swyxio swyxio closed this as completed Jan 11, 2019
@TheCeloReis
Copy link

Why not to use React.ComponentProps?

const defaultProps: React.ComponentProps<typeof Modal> = {
	title: "Hello World",
	visible: true,
	onClick: jest.fn(),
};

@swyxio
Copy link
Collaborator

swyxio commented Oct 2, 2020

@swyxio swyxio reopened this Oct 2, 2020
@stale
Copy link

stale bot commented Dec 1, 2020

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions!

@stale stale bot added the wontfix This will not be worked on label Dec 1, 2020
@swyxio swyxio closed this as completed in f29457f Dec 2, 2020
@swyxio
Copy link
Collaborator

swyxio commented Dec 2, 2020

@MarcelReis that was an excellent suggestion, thank you. i found our prior existing docs too complex for the common usecase, however Ferdy above is right that there are proptypes and defualtprops edge cases to consider if you want a fully reliable solution. i've opted for linking to this issue rather than bombarding our readers with complex utils right off the bat.

chef1119 pushed a commit to chef1119/chef-typescript that referenced this issue Oct 20, 2021
bernssolg added a commit to bernssolg/My-React-Sample that referenced this issue Feb 28, 2022
erinodev added a commit to erinodev/My-React-project that referenced this issue Feb 28, 2022
petardev101 added a commit to petardev101/react that referenced this issue Jun 4, 2022
supercrytoking added a commit to supercrytoking/react that referenced this issue Jul 14, 2022
kevindavies8 added a commit to kevindavies8/react-full-stack-developer that referenced this issue Aug 24, 2022
johnfrench3 pushed a commit to johnfrench3/react-Fronted-developer that referenced this issue Sep 7, 2022
ericbrown2716 added a commit to ericbrown2716/react-stack-build-website that referenced this issue Sep 29, 2022
peterjohnson4987 added a commit to peterjohnson4987/full-stack-developer-react that referenced this issue Oct 3, 2022
renawolford6 pushed a commit to renawolford6/react-husky-website that referenced this issue Oct 6, 2022
Yoshidayoshi23 added a commit to Yoshidayoshi23/react that referenced this issue Oct 20, 2022
renawolford6 added a commit to renawolford6/react-dev-build-doc- that referenced this issue Nov 10, 2022
coopfeathy added a commit to coopfeathy/cheatsheet that referenced this issue Dec 4, 2022
dreamcoder75 added a commit to dreamcoder75/react-sample that referenced this issue Jan 15, 2023
holyblock pushed a commit to holyblock/chart that referenced this issue Feb 27, 2023
AIDevMonster added a commit to AIDevMonster/Awesome-React that referenced this issue Jun 21, 2023
whiteghostDev added a commit to whiteghostDev/Awesome-React that referenced this issue Aug 6, 2023
cedev935 added a commit to cedev935/React-TypeScript that referenced this issue Sep 11, 2023
aleksandaralek added a commit to aleksandaralek/typescript-react-cheatsheet that referenced this issue Oct 24, 2023
xbucks pushed a commit to xbucks/react-cheatsheets that referenced this issue Oct 24, 2023
joyfulmagician added a commit to joyfulmagician/react that referenced this issue Oct 25, 2023
KonohaBrain125 pushed a commit to KonohaBrain125/React-Typescript that referenced this issue Oct 26, 2023
TOP-10-DEV added a commit to TOP-10-DEV/typescript-cheatsheets-react that referenced this issue Dec 8, 2023
secretsuperstar1109 added a commit to secretsuperstar1109/react-typescript-cheatsheets that referenced this issue Dec 9, 2023
champion119 added a commit to champion119/react that referenced this issue Jan 5, 2024
dragon360-dev added a commit to dragon360-dev/react that referenced this issue Mar 13, 2024
EugeneYoona added a commit to EugeneYoona/React_full_src that referenced this issue Apr 10, 2024
fairskyDev0201 added a commit to fairskyDev0201/typescript-cheatsheet that referenced this issue Apr 17, 2024
sloutionGuru added a commit to sloutionGuru/react that referenced this issue May 27, 2024
alisenola added a commit to alisenola/react-cheatsheets that referenced this issue May 29, 2024
marceloaguilera94 pushed a commit to marceloaguilera94/react that referenced this issue Jun 5, 2024
Linda423 added a commit to Linda423/React that referenced this issue Jul 31, 2024
touchsky941209 pushed a commit to touchsky941209/react-rebrand that referenced this issue Aug 26, 2024
genie4viz pushed a commit to genie4viz/react that referenced this issue Sep 2, 2024
miracle-soft-expert added a commit to miracle-soft-expert/React-TypeScript that referenced this issue Oct 15, 2024
chivalrousdev added a commit to chivalrousdev/React that referenced this issue Nov 14, 2024
Elegantdev23 added a commit to Elegantdev23/react-test that referenced this issue Dec 15, 2024
Roman-Sherman added a commit to Roman-Sherman/react-test that referenced this issue Dec 25, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
wontfix This will not be worked on
Projects
None yet
Development

No branches or pull requests

3 participants