2016-08-16 44 views
1

我想使用輸入來更新列表項的數量。現在我的代碼正確更新輸入值,但我用來生成列表的數組只是在兩次調用OnBlur事件時才更新。我不知道我失敗的地方。我使用了OnChange事件,問題是一樣的。 處理該更新邏輯功能是UpdateListReact函數更新狀態,當它被稱爲兩次

繼承人的jsBin工作http://jsbin.com/favabamitu/edit?html,js,console,output

這裏是我的代碼:

var InputData = React.createClass({ 
 
    getInitialState: function() { 
 

 
    return({ 
 
     number_of_locations: 4, 
 
     thickness_m: [] 
 
    }); 
 

 
    }, 
 

 
    componentDidMount: function() { 
 
    for (var i = 0; i < this.state.number_of_locations; i++) { 
 
     this.state.thickness_m.push(0); 
 
     console.log("state has been intitialized"); 
 
    } 
 
    }, 
 

 
    UpdateList: function(event) { 
 

 
    var value = event.target.value; 
 

 
    var locations = parseInt(this.state.number_of_locations); 
 
    console.log("local locations value is "+ locations); 
 
    var thickness = this.state.thickness_m; 
 

 
    if (locations > thickness.length) { 
 

 
     var dif = locations - thickness.length; 
 
     for (var i = 0; i < dif; i++) { 
 
     thickness.push(0); 
 
     } 
 
     console.log('up with' + dif + 'dif'); 
 

 
    } else if (locations < thickness.length) { 
 

 
     var dif = thickness.length - locations; 
 
     thickness.splice(0, dif); 
 
     console.log('down with' + dif + 'dif'); 
 

 
    } 
 

 
    this.setState({ 
 
     number_of_locations: value, 
 
     thickness_m: thickness 
 
    }); 
 
    }, 
 

 
    Lister: function(number, index) { 
 
    return (
 
     <li key = {index}> {number}</li> 
 
    ); 
 
    }, 
 

 
    render: function() { 
 
    var thickness_values = this.state.thickness_m 
 

 
    return (
 
     <div className="row"> 
 
      <div className="col-md-6"> 
 
       <div className="component"> 
 
        <div className="form-group"> 
 
         <label htmlFor="read-method">Reading Method</label> 
 
         <select className="form-control" name="read-method" id="read-method" required> 
 
          <option value="--">--</option> 
 
          <option value="1">Point Thickness Readings - PTR</option> 
 
          <option value="2">Critical Thickness Profiles - CTP</option> 
 
         </select> 
 
        </div> 
 
        <div className="form-group"> 
 
         <label htmlFor="number-of-locations">Number of Locations</label> 
 
         <input onBlur={this.UpdateList} 
 
          defaultValue={this.state.number_of_locations} 
 
          className="form-control" 
 
          type="number" 
 
          max="50" 
 
          min="1" 
 
          id="number-of-locations"/> 
 
        </div> 
 
        <div className="form-group"> 
 
         <label htmlFor="separation">Separation</label> 
 
         <input className="form-control" type="number" id="separation"/> 
 
        </div> 
 
        <div className="form-group"> 
 
         <ul> 
 
          {thickness_values.map(this.Lister)} 
 
         </ul> 
 
         <small>your list now has {this.state.number_of_locations} items</small> 
 
        </div> 
 
       </div> 
 
      </div> 
 
      <div className="col-md-6"> 
 
       hello 
 
      </div> 
 
     </div> 
 
    ) 
 
    } 
 
}) 
 

 

 

 
ReactDOM.render(<InputData /> , 
 
    document.getElementById('input-measurement-data') 
 
);
<div id="input-measurement-data"> 
 
    <!-- This element's contents will be replaced with your component. --> 
 
</div> 
 

 
<script src="https://cdnjs.cloudflare.com/ajax/libs/react/15.1.0/react.min.js"></script> 
 
<script src="https://cdnjs.cloudflare.com/ajax/libs/react/15.1.0/react-dom.min.js"></script>

任何幫助將不勝感激!

+0

如果有什麼東西需要運行,調試會更容易。你可以把代碼放在jsfiddle上嗎? – Chris

+0

讓我更新它:) –

+0

它似乎並沒有運行。檢查控制檯。 – Chris

回答

2

改變這一行:

var locations = parseInt(this.state.number_of_locations); 

var locations = parseInt(value); 

對於您的代碼,它在接下來的onChange更新,因爲你是在功能UpdateList末更新this.state.number_of_locations但在計算前的位置數此更新。

BTW,之前位置計算更新這個狀態不會幫助,因爲this React feature

的setState()不會立即發生變異this.state而造成掛起狀態轉變。調用此方法後訪問this.state可能會返回現有值。

+0

非常感謝!它正在工作:D –

+0

在這個問題中有類似的問題,評論和答案包含一些額外的背景:http://stackoverflow.com/q/34442806/1424833 –