首页 > 解决方案 > 无法读取未定义反应的属性“长度”

问题描述

TypeError: Cannot read property 'length' of undefined

这就是我运行我的反应应用程序时编译器所说的。我需要做什么?

request = (start,end) => {
   if(this.state.teams.length < 1){
       axios.get(`${ URL }/teams`)
       .then( response => {
           this.setState({
               teams:response.data
           })
       })
   }

    axios.get(`${ URL }/articles?_start=${start}&_end=${end}`)
    .then( response => {
        this.setState({
            items:[...this.state.items,...response.data]
        })
    })
}

标签: javascriptreactjs

解决方案


我建议先检查道具是否未定义或为空甚至已声明。

例如:-

    const card = props && props.cards && props.cards.length > 0 ?
        props.cards.map((card, i) => {
            return (
                <card >
            )
        }) : '';

并退还您的卡。


推荐阅读