2017-10-04 35 views
0

我所遇到當我用選擇器組件,如下所示該錯誤,「未定義不是(評價‘child.props.value’)的對象」在反應天然

<Picker 
    style={{ flex: 1 }}> 
    selectedValue={this.props.shift} 
    onValueChange={value => this.props.employeeFormAction({ 
              prop:'shift', value })} 
> 
     <Picker.Item label='Monday' value='Monday' /> 
     <Picker.Item label='Tuesday' value='Tuesday' /> 
     <Picker.Item label='Wednesday' value='Wednesday' /> 
     <Picker.Item label='Thursday' value='Thursday' /> 
     <Picker.Item label='Friday' value='Friday' /> 
     <Picker.Item label='Saturday' value='Saturday' /> 
     <Picker.Item label='Sunday' value='Sunday' /> 
</Picker> 

餘誤差選擇器部件已經厭倦了來自同一社區

react-native - Picker - undefined is not an object (evaluating 'this.props.children[position].props)

這一解決方案,但它並沒有爲我工作。任何機構能否爲這個問題提出解決方案。

+0

嘗試設置一個選擇器項的關鍵。 – vijayst

+0

@vijayst,即使在將選項添加到鍵後也沒有結果。任何選擇? – Kishore

回答

1

儘量不要硬編碼值。這種方式更清潔:

// inside your component (supposing is not a functional component) 
_renderShifts =() => { 
    const shifts = ['Monday', 'Tuesday', 'Wednesday','Thursday', 
       'Friday','Saturday', 'Sunday']; 

    return shifts.map((shift) => { 
    <Picker.Item key={"shift_"+i} label={shift} value={shift} /> 
    }); 
} 


// now your picker should render this way 
_renderPicker =() => { 
    <Picker 
    style={{ flex: 1 }}> 
    selectedValue={this.props.shift} 
    onValueChange={value => this.props.employeeFormAction({prop:'shift', value })} 
    > 
    {this._renderShifts()} 
    </Picker> 
} 
+0

儘管我已經刪除了硬編碼的值,但我仍然面臨同樣的錯誤。你能建議任何替代方法嗎? – Kishore

+0

你能否提出任何替代方案,因爲我仍然面臨問題。 – Kishore

+0

您是否在使用redux來管理更改,如果this.props.shift未定義,您可以嘗試使用console.log嗎?順便說一句,你是否因爲某些特定的原因給它彈性1? – EnriqueDev

相關問題