React Native无法读取undefined的属性

时间:2017-05-18 22:55:19

标签: reactjs react-native

尝试使用api调用来呈现listview

    (rowData)=><CatalogCell newWidth={imgWidth} imgMargin = 
    {imgMargin} 
    url={rowData.product.name} price={rowData.price}/>

我可以正确阅读价格,但是如果我读到超出深度1的任何东西,我得到了这个错误,基本上rowData.product不会给我任何东西,但如果我去rowData.product,它将显示错误

得到了

Cannot read property 'name' of undefined

renderRow
    CatalogList.js:74:114
StaticRenderer.render
    StaticRenderer.js:32:22
<unknown>
    ReactCompositeComponent.js:1218:19
measureLifeCyclePerf
    ReactCompositeComponent.js:63:11
ReactCompositeComponentWrapper._renderValidatedComponentWithoutOwnerOrContext
    ReactCompositeComponent.js:1217:24
ReactCompositeComponentWrapper._renderValidatedComponent
    ReactCompositeComponent.js:1249:31
ReactCompositeComponentWrapper.performInitialMount
    ReactCompositeComponent.js:499:29
ReactCompositeComponentWrapper.mountComponent
    ReactCompositeComponent.js:346:20
Object.mountComponent
    ReactReconciler.js:56:34
Constructor.mountChildren
    ReactMultiChild.js:276:41

设置列表视图数据的代码:

componentDidMount(){
        getCatalog((callback)=>{
            console.warn(callback.result.data)
            this.setState({
                dataSource:this.state.dataSource.cloneWithRows(
                   callback.result.data
               ),
                loading:false,
            })
        })
    }

1 个答案:

答案 0 :(得分:0)

Nvm,应该有一个标志,告诉数据是否准备就绪。深度1起作用,因为道具始终存在