2017-10-04 77 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

相关问题