K
K
Kyki42021-07-11 20:07:15
typescript
Kyki4, 2021-07-11 20:07:15

What is a prop in TypeScript React?

I recently started studying TS and wondered how to pass all the properties of a regular button to the custom Button component, which I did, but I constantly came across the concept of "support" on many sites and, despite the code examples, I did not understand what it was exactly.
Examples from context:

Interface Extension
This is something that took me a while to learn, but is very helpful in creating managed interfaces. Let's add support for passing any other PROP to the button. It may seem easy to add [key:string]:any to the interface to make it simpler, but instead we should extend our interface with the correct button interface from React to get full types.

60eb21a0b9130838594412.png

Passing a tag as a PROP:

<ChildComponent tag="h1" />

And also whether this concept is connected with TS || React or I just haven't met it before.

Sites where I came across this:

1 example 1 (VPN is needed)

2 example 2

3

Answer the question

In order to leave comments, you need to log in

3 answer(s)
A
acwartz, 2021-07-13
@Kyki4

coderoad, quora and other similar Russian-language Q&A are clones of the English-language Stackowerflow in machine translation, and very, very poor at that. SO has a Russian-language section, but for some reason it is not copied.
The second link is actually this: https://stackoverflow.com/questions/51871080/can-i... and it becomes clear that the question is "How to forward an HTML tag as an attribute value and then use it as a property (prop) ?"

A
Aetae, 2021-07-11
@Aetae

Huh, never heard of it. True, I read on the topic almost exclusively in English.)
Otherwise, the essence is simple: JSX is a dialect of js, despite the html-like notation, it is explicit and without any special tricks javascript. This means that all the features of TypeScript are applicable to it. You need a different set of props depending on one particular one - use union and generics, you need to get a set of properties of some foreign component - use , etc. etc. React.ComponentProps<typeof ForeignComponent>

A
Alex, 2021-07-11
@Kozack

https://ru.reactjs.org/docs/components-and-props.html

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question