ReactJS when checking HTML attribute, responsejs does not allow select checkbox

As in the title of the question, I cannot mark the checkbox as selected in the onChange event of this selectbox, for example,

<select id='featuresType' onChange={this.handleChange.bind(this, 'type')} > {this.getOptions(featuresType)} </select> 

were like handleChange:

  handleChange: function (field, e) { if (field == 'type') { let selected = e.target.value; this.setState({typeSelected: selected}) } /*Why the console is taking the previous selecte option, ex: if Locker is selectec below lone prints 'dc' */ console.log(field ,',', this.state.typeSelected) }, 

when it binds to defaultValue,

defaultChecked={(this.state.typeSelected =='locker')}

  • The selection field cannot indicate the specified condition, but manually checked / notchecked can be checked.

checked={(this.state.typeSelected =='locker')}

  1. In this checked attribute, the flag can be marked as checked, based on the value of the selection box option. BUT in this case, a manual verification method or verification does not occur.

Hope you guys had a problem.

Here is the JSFiddle

I wanted the flag to work in both directions, when choosing the option, the flag should be checked. And also the user can check / uncheck Manully

+5
source share
1 answer

React Docs says :

setState () does not immediately mutate this.state, but creates a pending state transition. Accessing this.state after calling this method can potentially return an existing value.

There is no guarantee that setState calls will work synchronously, and calls can be collected to improve performance.

+1
source

Source: https://habr.com/ru/post/1242407/


All Articles