...

/

Handling Props with the Same Property Name

Handling Props with the Same Property Name

In this lesson, we'll see how two prop properties with the same name can be invoked at once.

We allowed passing custom props into the getToggler props getter. Now, what if the user wanted to pass in a custom onClick prop?

Here’s what I mean:

.Expandable-panel {
    margin: 0;
    padding: 1em 1.5em;
    border: 1px solid hsl(216, 94%, 94%);;
    min-height: 150px;
  }

If the user does this, it completely overrides the onClick handler within the useExpanded custom hook and breaks the functionality of the app as you can see in the output.

The button no longer expands the element, but the user’s custom click handler is invoked whenever you click the button.

Why is this the case?

This is because we have an onClick property in the returned object, but it is overridden by the user.

Press + to interact
// useExpandable.js
...
const getTogglerProps = useCallback(
({ ...customProps }) => ({
onClick: toggle, // this is overriden
'aria-expanded': expanded,
// customProps overrides the onClick above
...customProps
}),
[toggle, expanded]
)

Overriding the User’s onClick Handler

If you move the ...

Access this course and 1400+ top-rated courses and projects.