简体   繁体   中英

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 eG *props.lastName;

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

Is this possible? If yes, how?

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.

Only like this:

const ExampleComponent = (props) => {
    {name, age} = props;
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM