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;
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.
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.
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.
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With