2017-09-14 124 views
0

我有一個規則組件,其中可以添加任意數量的規則,並且添加的規則將隨規則的額外添加功能一起顯示。在添加,刪除規則字段時沒有問題,但是當將數據發佈到服務器時,數據會發布,但客戶端會生成一個額外的空字段。我不知道如何以及爲何發生。點擊保存按鈕添加額外的一個空字段

下面是詳細的代碼

const _id = 0; 

function guid() { 
    function s4() { 
    return Math.floor((1 + Math.random()) * 0x10000).toString(16).substring(1); 
    } 
    return s4() + s4(); 
} 

const removeByKey = (obj, deleteKey) => 
    Object.keys(obj).filter(key => key !== deleteKey).reduce((result, current) => { 
    result[current] = obj[current]; 
    return result; 
    }, {}); 

class Rules extends React.PureComponent { 
    constructor(props, context) { 
    super(props, context); 

    this.state = { 
     rules: { ruleAndregulations: {} }, 
    }; 
    } 

    componentDidMount() { 
    this.props.loadRules(); 
    } 

    componentWillReceiveProps(nextProps) { 
    if (nextProps.rules.size && nextProps.rules !== this.props.rules) { 
     nextProps.rules 
     .entrySeq() 
     .map(([key, value]) => { 
      console.log('key', key, value); 
      this.setState(
      state => ({ 
       rules: { 
       ...state.rules, 
       ruleAndregulations: { 
        ...state.rules.ruleAndregulations, 
        [value.get('_id')]: { _id: value.get('_id'), value: value.get('value') } 
       } 
       } 
      }) 
     ); 
     }) 
     .toArray(); 
    } 
    } 

    handleAddRules = e => { 
    this.setState({ 
     rules: { 
     ...this.state.rules, 
     ruleAndregulations: { 
      ...this.state.rules.ruleAndregulations, 
      [guid()]: { 
      _id: guid(), 
      value: '' 
      } 
     } 
     } 
    }); 
    }; 

    handleRemoveRules = (e, num) => { 
    this.setState({ 
     rules: { 
     ...this.state.rules, 
     ruleAndregulations: removeByKey(this.state.rules.ruleAndregulations, num) 
     } 
    }); 
    }; 

    handleChange = e => { 
    this.setState({ 
     rules: { 
     ...this.state.rules, 
     ruleAndregulations: { 
      ...this.state.rules.ruleAndregulations, 
      [e.target.name]: { 
      _id: e.target.name, 
      value: e.target.value 
      } 
     } 
     } 
    }); 
    }; 

    handleSave = e => { 
    e.preventDefault(); 
    const ruleObj = { rule_regulations: [] }; 
    const { ruleAndregulations } = this.state.rules; 
    Object.keys(ruleAndregulations).map(val => 
     ruleObj.rule_regulations.push(ruleAndregulations[val].value) 
    ); 
    this.props.postRules(ruleObj); 
    }; 

    render() { 
    const { rules } = this.state; 
    console.log('state', this.state); 
    return (
     <div className="basic-property"> 
     Add Rules and Regulations 
     <span className="circleInputUi" onClick={this.handleAddRules}> 
      + 
     </span> 
     {rules && 
      rules.ruleAndregulations && 
      <RulesInputContainer 
      handleChange={this.handleChange} 
      handleRemoveRules={this.handleRemoveRules} 
      handleSave={this.handleSave} 
      value={rules.ruleAndregulations} 
      />} 
     </div> 
    ); 
    } 
} 

export default connect(mapStateToProps, mapDispatchToProps)(Rules); 

減速機發布是

case POST_RULES_SUCCESS: 
     return state 
     .merge({ 
      loading: false, 
      error: null, 
      response: action.response.message 
     }) 
     .update('rules', rules => rules.push(fromJS(action.response.data))); 

代碼

https://gist.github.com/MilanRgm/7f744ee27df7e5d17410ffcba99fb89c

張貼我得到componentWillReceiveProps

以下數據後

enter image description here

當使用的concat

a)在第一個規則被添加

enter image description here

b)如果第二規則添加

enter image description here

回答

0

它看起來像你是push列表成列表,而不是concat。在你減速,儘量

.update('rules', rules => rules.concat(fromJS(action.response.data))); 
+0

當我使用CONCAT並添加規則,例如有alread一個規則,如果我添加另一個規則說規則2,然後我得到規則1,規則2,規則1,規則2 – milan

+0

更新了我問題 – milan

+0

@milan什麼是後端返回,所有規則,或只是您發佈的規則? –

相關問題