Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save DoctorDerek/aba7492bc6c7fcb592b0d5f5e3e60a43 to your computer and use it in GitHub Desktop.
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
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