Home > Mobile >  Typescript React: Use props AND (!) destructuring
Typescript React: Use props AND (!) destructuring

Time:12-05

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?

CodePudding user response:

You can do it like this:

 const ExampleComponent = ({name, age, ...props}) => {
    // do something
 }

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

  • Related