AjackX
AjackX

Reputation: 21

React JS need to set multiple state values from dropdown item list

I have a dropdown list from an array of values like mapped to a dropdown and a handleSelect

const handleSelect = (e) => {
     handleShow()
    setCommunityName(e)
}
<DropdownButton id="dropdown-basic-button" title="Join Community" onSelect={handleSelect} hidden={showJoin}>
    {communities.map(data =>
    <Dropdown.Item title={data.CommunityName} key={data.id}
    eventKey={data.CommunityName}>{data.CommunityName}</Dropdown.Item>)}
</DropdownButton>

Right now only data.CommunityName is set in state, but I would also like to set the state of data.id as well. I was trying to do something like this?

const handleSelect = (e, i) => {
    handleShow()
    setCommunityName(e)
    setCommunityId(i)
}

Upvotes: 0

Views: 47

Answers (1)

user6749601
user6749601

Reputation:

What about setting the entire object as value?

<Dropdown.Item title={data.CommunityName} key={data.id}
eventKey={data}>{data.CommunityName}</Dropdown.Item>)}
const handleSelect = (e) => {
    handleShow()
    setCommunityName(e.CommunityName)
    setCommunityId(e.id)
}

Upvotes: 1

Related Questions