Created
October 12, 2022 16:28
-
-
Save DoctorDerek/aba7492bc6c7fcb592b0d5f5e3e60a43 to your computer and use it in GitHub Desktop.
Partial Object Destructuring of React Props With ... Rest Parameters Syntax https://medium.com/p/4e8629a02035
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function ChildComponent(props) { | |
// There could be other props that we didn't destructure here. | |
const { firstName, lastName } = props | |
return ( | |
<span> | |
{firstName} {lastName} | |
</span> | |
) | |
} | |
function ParentComponent(props) { | |
// <ParentComponent> partially destructures the props. | |
const { firstName } = props | |
// We pass the remaining props (lastName) via ... spread. | |
return <ChildComponent firstName={firstName} {...props} /> | |
} | |
export default function GrandParentComponent(props) { | |
// In this component, we don't use the input `props` at all. | |
return <ParentComponent firstName="Johnny" lastName="Walker" /> | |
} | |
// The rendered result will be <span>Johnny Walker</span>. |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment