2017-08-15 73 views
0

所以我的工作場所在一個組件內使用SelectField,這個組件在多個地方使用。說得通。現在,當我在當前正在使用的那個組件中使用該組件時。我創建的onChange事件不會傳遞正確完成所需的值。現在,如果我將SelectField更改爲onChange = {onChange},它可以正常工作。但是,它不能以這種方式設置,因爲它內部的其他部分依賴於它。我是React新手。這裏是我的功能onChange在SelectField組件中Material-UI/React不接受來自父組件的onChange

addHistory = (field) => (event, index, value, target) => { 
    const { history } = this.state; 
    console.log(this.state.history); 
    console.log(field); 
    if (field === 'employerName' || field === 'description') { 
     history[field] = event.target.value; 
    } else if (field === 'roles') { 
     history[field] = value; 
    } else { 
     history[field] = event.value; 
    } 
    this.setState({ 
     history: this.state.history, 
    }); 
    } 

這裏是我的導入和與組件

<CompanySelectField 
    label={translations.translate('driverProperties', 'typeOfVehicle')} 
    hint={translations.translate('driverProfile', 'pleaseSelectRole')} 
    style={{ marginTop: '25px' }} 
    name={'roles'} 
    value={this.state.history.roles} 
    onChange={this.addHistory('roles')} 
    fullWidth 
    required 
    > 

這裏使用CompanySelectField是CompanySelectField本身

<SelectField 
    name={name} 
    maxHeight={300} 
    value={value} 
    onChange={(event, index, response) => onChange(response)} 
    disabled={disabled} 
    hintText={hint} 
    > 

謝謝你的時間提前。

+0

如果我正確地理解了你,那麼改變SelectField的onChange這個prop應該可以解決你的問題=> onChange = {(event,index,response)=> onChange(event,index,response)} –

+0

完美,謝謝 –

回答

0

你從addHistory返回的方法有4個參數,即event, index, value, target,而你從SelectField組件調用的那個,即onChange={(event, index, response) => onChange(response)}它只有一個參數,並且這個響應也與上面提到的第一個參數不匹配返回的方法。

+0

謝謝,這是非常有意義的,我覺得沒有意識到這一點是愚蠢的。謝謝你的回答。 –

+0

沒有probs ...快樂是我的 –