Discussion about this post

User's avatar
Weiping (Larry) Ding's avatar

Thanks for sharing good exercise!

My first thinking is to use custom hook to extract away these stateful logic, normalise the state needed by the component.

Inside it handles these mapping, exclusion and return a normalised type

type IssueItem {

name: string

url?: string

}

Imagine somewhere inside a certain container component:

const items: []IssueItem = useIssueItems(data: IssueResponse)

Then pass it down to context menu rendering, it's just simple mapping based on the new type

const IssueContextMenu = ({ items }: { items: []IssueItem }) => {

return (

<DropdownMenu>

items.map(item => {item.url ? < LinkMenuItem url={url} /> : < ButtonMenuItem />} )

</DropdownMenu>

)

}

Now component is easier to read, also it decouples it from specific response shape. This could be very helpful if we show menu in different page, when api response changed, we only need to update mapping logic in this single custom hook. All menu should still be able to render.

Keen to check your solution later.

Expand full comment

No posts