Why doesn't this select list render with the correct item selected based on the defaultValue

前端 未结 1 1396
孤街浪徒
孤街浪徒 2021-02-19 04:19

I have a ReactJS component:

var RegionsList = React.createClass({

    handleChange: function () {
        var regionId = this.refs.userRegions.getD         


        
相关标签:
1条回答
  • 2021-02-19 04:41

    When the <select> is initially mounted, the default value is ''. Once an uncontrolled form component is in the DOM, React doesn't look updates to the defaultValue prop. In this case it looks like your intention is to always have the selectedRegionId state match what's shown to the user, so you may want to change defaultValue to value and add a this.setState({selectedRegionId: regionId}); call to your onChange handler; then your component state and the DOM will always be in sync.

    0 讨论(0)
提交回复
热议问题