emplo yee
emplo yee

Reputation: 235

Typescript + React: Use props AND (!) destructuring

I know that it´s possible to use destructuring for props in React. However, can I use destructuring AND props? So that I have some determined inputs and also some addtional ones that I access via props

Example


This is the expected output. So I can use name and age directly, and everything else via props e.G. *props.lastName;

const ExampleComponent : React.FC<{name: string, age:number}> = ({name, age}, props) => {
    // do something
}

Is this possible? If yes, how?

Upvotes: 1

Views: 725

Answers (1)

kian jalilian
kian jalilian

Reputation: 294

You can do it like this:

const ExampleComponent: React.FC<{ name: string; age: number }> = ({
  name,
  age,
  ...props
}) => {
  // do something
};

This works with typescript too. You can just type check name and age.

Upvotes: 4

Related Questions