react改变state里得数值得问题

代码如下:

//点击事件

longBtnClick=()=>{
     
        this.setState((prev)=>({
            ifNextScenes:true,
            step:prev.step+1   //增加step得数值,默认是0

        }),()=>{
            setTimeout(()=>{
                this.setState({
                    ifNextScenes:false
                })
            },300);
        })
    }

//render

<img  className="mainImg" src={
     this.state.onSwitch?this.props.data[parseInt(this.state.step)].text   //这里报错了,此时this.state.step应该时等于1
     :
     this.props.dataT[parseInt(this.state.step)]
    } /> 

问题:在第一遍默认加载得时候,一切正常,但当点击时,提示如下错误:

Uncaught TypeError: Cannot read property '1' of undefined

为什么会报1得错误,它只是个下标又不是变量?数组时确认有1下标得。

阅读 2.3k
4 个回答

这个报错是下标前的变量是 ubdefined,也就是 this.props.data 是 undefined

报错的意思是你的data属性不存在

新手上路,请多包涵

this.props.data 数据没有获取到

新手上路,请多包涵

出现这个错误的原因是你的this.props.data或者this.props.dataT中有一个是undefined,你可以把这两个值打印出来看看就知道了

撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题