H
H
herasko-sg2021-10-15 00:41:11
typescript
herasko-sg, 2021-10-15 00:41:11

How to pass an interface (typing) to a React component?

How to pass an interface (typing) to a React component?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
Alexandroppolus, 2021-10-15
@Alexandroppolus

A component is defined by its props. Usually they do this (types are taken as an example):

// пропсы
type MyCompProps = {
  value: string;
  ...
};

// для компонента указывается тип - "функциональный компонент с пропсами MyCompProps"
// далее не нужно указывать тип для props и тип возвращаемого значения - TS выводит сам.
export const MyComp: React.FC<MyCompProps> = (props) => {
  ...
  return (<div>{props.value}</div>);
};

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question