I am using React-Bootstrap FormControl dropdown. In this DD I applied map function which iterates and provides the dropdown options.
I am not getting how to set the value of selected option. I tried to use onChange
and onSelect
. On change I called following line of code:
Below is simple example of drop which is used in Reactjs using React-Bootstrap:
export default class SignUp extends Component {
constructor(props, context) {
super(props, context);
this.handleChange = this.handleChange.bind(this);
this.register =this.register.bind(this);
this.handleChangeDate = this.handleChangeDate.bind(this);
this.state = {
gender:'',
genderCat: [],
}
handleChangeGender(){
const name = e.target.name;
const value = e.target.value;
console.log(name, value);
this.setState({[name]: value});
console.log(this.state);
}
componentDidMount() {
api to get gender => GetData('getGender').then( (result) => {
this.setState({
genderCat: result
});
// this.state.data = result.message_text;
// console.log(data.message_text);
})
render() {
<FormControl
name=gender
componentClass=select
placeholder=select
onChange={this.handleChangeGender}
>
{
this.state.genderCat.map((data) =>
<option key={data.id} value={data.value} >
{data.name}</option>
)
}
<FormControl.Feedback />
</FormControl>
}
}
How to set state value here ? I am getting one step behind state value.Like for very first time if I select Male (coming from map function, its value is 1) then then in state its give null, but then onchange female its taking 1, now if I change to other then its takes 2 ( its value of female)