Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to set selected item in reactstrap Dropdown?

Tags:

How to set selected item in reactstrap Dropdown?

There is example of dropdown: https://reactstrap.github.io/components/dropdowns/

When I select item in dropdown, it is not displayed.

*******************Working solution*****************************

import React from "react"; import {ButtonDropdown, DropdownItem, DropdownMenu, DropdownToggle} from "reactstrap"; import superagent from "superagent";  class BootstrapSelect extends React.Component {      constructor(props) {         super(props);          this.toggle = this.toggle.bind(this);         this.changeValue = this.changeValue.bind(this);         this.state = {             actions: [],             dropDownValue: 'Select action',             dropdownOpen: false         };     }      toggle(event) {          this.setState({             dropdownOpen: !this.state.dropdownOpen         });     }      changeValue(e) {         this.setState({dropDownValue: e.currentTarget.textContent});         let id = e.currentTarget.getAttribute("id");         console.log(id);     }       componentDidMount() {         superagent             .get('/getActions')             .type('application/json; charset=utf-8')             .end(function (err, res) {                 console.log(res.body);                 this.setState({actions: res.body});             }.bind(this));      }      render() {         return (             <ButtonDropdown isOpen={this.state.dropdownOpen} toggle={this.toggle}>                 <DropdownToggle caret>                     {this.state.dropDownValue}                 </DropdownToggle>                 <DropdownMenu>                     {this.state.actions.map(e => {                         return <DropdownItem id={e.id} key={e.id} onClick={this.changeValue}>{e.name}</DropdownItem>                     })}                 </DropdownMenu>              </ButtonDropdown>         );     }  }  export default BootstrapSelect; 
like image 922
user657009 Avatar asked Jun 05 '17 08:06

user657009


People also ask

How do I use dropdown in Reactstrap?

The Dropdown component is used to pass the isOpen & toggle props via context to the following components: DropdownToggle , DropdownMenu . The DropdownToggle uses the Button component internally, meaning it also accepts all the props the Button component accepts.

How do you show the selected value in a dropdown react?

Controlled Components to Get Dropdown Menu Value in React First, we must set up an event listener on the <select> element and define an event handler. In our example, it takes one argument - e (short for event) and calls the setFruit() function. To set the state with the selected value, we access the e. target.

How do I close a dropdown after selection in react?

If the user clicks outside of the dropdown it will stay open. In order to close the menu they'd have to go click on the menu button again.

How do you use the dropdown button in react?

Dropdown Component provides a way to displaying lists of links or more actions within a menu when clicked over it. We can use the following approach in ReactJS to use the react-bootstrap Dropdown Component. Dropdown Props: alignRight: It is used to align the menu to the right side of the Dropdown toggle.


1 Answers

Add an onclick on your DropDownItem (inside a div ?) to change your state. Set a "dropDownValue" from your click event. In your dropDownToggle, get your state.dropDownValue.

Something like this :

changeValue(e) {   this.setState({dropDownValue: e.currentTarget.textContent}) }  <DropdownToggle caret>     {this.state.dropDownValue} </DropdownToggle> <DropdownItem>     <div onClick={this.changeValue}>Another Action</div> </DropdownItem> 

Of course, don't forget to init it and bind the function for your this to work.

like image 66
Nevosis Avatar answered Sep 21 '22 04:09

Nevosis