ReactStrap: Accessing the id of the selected dropDownItem

我的未来我决定 提交于 2020-03-06 10:29:26

问题


I have this dropdownList:

  <Dropdown
                  isOpen={this.state.dropdownOpen[3]}
                  toggle={() => {
                    this.toggle(3);
                  }}
                >
                  <DropdownToggle className="my-dropdown" caret>
                    {" "}
                    {this.state.dropDownValue}
                  </DropdownToggle>
                  <DropdownMenu>
                    <DropdownItem productid="0">
                      {" "}
                      <div onClick={this.changeValue}>
                        PURE BLACK MAT BLUETOOTH
                      </div>
                    </DropdownItem>
                    <DropdownItem productid="1">
                      {" "}
                      <div onClick={this.changeValue}>
                        PLAYER BLACK MAT FILAIRE
                      </div>
                    </DropdownItem>
                    <DropdownItem productid="2">
                      {" "}
                      <div onClick={this.changeValue}>
                        PURE PLUS BLACK MAT MEMORY 128 G
                      </div>
                    </DropdownItem>
                    <DropdownItem productid="3">
                      {" "}
                      <div onClick={this.changeValue}>
                        PURE LIMITED CHROME GOLD MEMORY 256G
                      </div>
                    </DropdownItem>
                    <DropdownItem productid="4">
                      {" "}
                      <div onClick={this.changeValue}>
                        PURE GOLD MAT BLUETOOTH
                      </div>
                    </DropdownItem>
                    <DropdownItem productid="5">
                      {" "}
                      <div onClick={this.changeValue}>
                        PLAYER GOLD MAT FILAIRE
                      </div>
                    </DropdownItem>
                    <DropdownItem productid="6">
                      {" "}
                      <div onClick={this.changeValue}>
                        PURE PLUS GOLD MAT MEMORY 128 G
                      </div>
                    </DropdownItem>
                    <DropdownItem productid="7">
                      {" "}
                      <div onClick={this.changeValue}>GAME ONE WHITE</div>
                    </DropdownItem>
                  </DropdownMenu>
                </Dropdown>

And this onClickListener:

 changeValue(e) {
    console.log(
      "The selected productId is: ",
      e.currentTarget.getAttribute("productid")
    );
    //The selected productId is:  null
    this.setState({
      dropDownValue: e.currentTarget.textContent
    });
  }

I am trying to access the productid of the selected DropdownItem. But, for some reason the result is always null:

console.log( "The selected productId is: ", e.currentTarget.getAttribute("productid") ); //The selected productId is: null

Any idea what I've done wrong?


回答1:


I have fixed this by passing the attribute productid inside the div:

 <DropdownItem>
                      {" "}
                      <div productid="0" onClick={this.changeValue}>
                        PURE BLACK MAT BLUETOOTH
                      </div>
                    </DropdownItem>


来源:https://stackoverflow.com/questions/60435037/reactstrap-accessing-the-id-of-the-selected-dropdownitem

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!