值明明有为什么还是undefined

  render(){
    var styleObj = {};
   
    //如果props中制定了这张图片的位置信息,则使用
     console.log(this.props.imgState.pos);
    if(this.props.imgState.pos){
      styleObj = this.props.imgState.pos;
    }
   
      return(
      <figure className = 'img-figure' style = {styleObj}>
        <img src = {this.props.imageData.imageURL} alt = {this.props.imageData.title}/>
        <figcaption>
          <h2 className = "img-title">{this.props.imageData.title}</h2>
        </figcaption>
      </figure>
      )
  }

图片描述

请问这里值都输出了,为什么还是报错?

阅读 4k
2 个回答

代码不全,没法判断。

说白了,imgState这个属性在某一时刻被设为了undefined。

你代码不全,我说个大概吧——

你的父组件应该在更新子组件的imgState属性的时候,imgState的值变成了undefined。你看一下 父组件 imgState 做了哪些更新操作。

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