S
S
Stepan Turchenko2020-10-13 15:41:03
typescript
Stepan Turchenko, 2020-10-13 15:41:03

How to get all props of a React component by its instance?

We have typescript and two components.
I want component 2 to require all the same values ​​in props as component 1.
How I imagine it:

import {Button} from 'react-native';
const MyButton = (props: typeof Button) => {
  return <Button title={props.title} onPress={props.onPress} style={{color:'#fff'}} />
}

(let's imagine that we don't have predefined types like ButtonPropsTypes (for example, if we use a button from some design library))

Answer the question

In order to leave comments, you need to log in

1 answer(s)
D
Dmitry Belyaev, 2020-10-13
@StepanTur

import type {ComponentType} from 'react';
import {Button} from 'react-native';

type InferProps<Component> = Component extends ComponentType<infer Props> ? Props : never;

const MyButton = (props: InferProps<typeof Button>) => {
  return <Button title={props.title} onPress={props.onPress} style={{color:'#fff'}} />
}

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question