未捕获的类型错误:无法读取未定义的属性“_props”

米娜

我试图将一些数据从 state 中作为道具传递给另一个组件,如下所示:

class Editor extends React.Component {
  constructor(props, context) {
    super(props, context);
    this.state = {
      Data: null
    };
  }

  handleClick = () => {
    const fdata = this.props.fetchData(); //returns some data as array of objects
    this.setState({
      Data: fdata
    });
  };

  render() {
    <Overview sos={this.state.Data} />; //trying to pass Data from state to another component
  }
}

出错Uncaught TypeError: Cannot read property '_props' of undefined

阿廷辛格 |

首先让你的状态对象变量以小写字母开头

this.state = {data: null}

其次,您的 handleClick 具有异步逻辑,因此请使用 async 和 await。像这样 -

handleClick = async () =>{
  const fdata = await this.props.fetchData(); //returns some data as array of objects
  this.setState({
     data: fdata
  });
}

编辑:正如@rrd 提到的,你需要在渲染中返回 jsx

render{
  return(
     ....
 )
}

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

我可以在 componentDidMount 中使用“this.props”吗?错误:未捕获的类型错误:无法读取未定义的属性“地图”

Chrome 扩展类型错误:未捕获的类型错误,无法读取未定义的属性“绑定”

错误错误:未捕获(承诺):类型错误:无法读取未定义的属性“标题”

角度:错误错误:未捕获(承诺):类型错误:无法读取未定义的属性“设置”

未捕获的类型错误:无法读取未定义的属性(读取“toString”)

未捕获的类型错误:无法读取未定义的属性(读取“stopPropagation”)

未捕获的类型错误:无法读取未定义的属性(读取“url”)

未捕获的类型错误:无法使用 BrowserRouter 读取未定义的属性(读取“路径名”)

未捕获的类型错误:无法读取未定义的属性(读取“开始”)

firebase - 未捕获的类型错误:无法读取未定义的属性(读取“选项”)

未捕获的类型错误:无法读取未定义的属性(读取“包含”)

未捕获的类型错误:无法读取未定义的属性(读取“样式”)

未捕获的类型错误:无法读取未定义的属性(读取“源”)

“未捕获的类型错误:单击数字按钮时无法读取未定义的属性(读取 'toString')

未捕获的类型错误:无法在 fullcalendar 中读取未定义的属性(读取“hasTime”)

未捕获的类型错误:无法读取未定义的属性(读取“img”)

未捕获的类型错误:无法在第 13:1 行读取未定义的属性(读取“0”)

未捕获的类型错误:无法读取未定义的属性(读取“每日确认”)

未捕获的类型错误:无法读取 li 的未定义(读取“样式”)数组的属性

JSON - 未捕获的类型错误:无法读取未定义的属性(读取“0”)

Javascript 错误:未捕获的类型错误:无法读取未定义的属性“长度”

错误:未捕获(承诺):类型错误:无法读取未定义的属性“pushTag”

Javascript 拆分错误:未捕获的类型错误:无法读取未定义的属性“拆分”

未捕获的类型错误:无法读取文本字段上未定义错误的属性“toLowerCase”

轨道控件显示错误未捕获的类型错误:无法读取未定义的属性“旋转”

jQuery错误未捕获的类型错误:无法读取未定义的属性“长度”

未捕获的类型错误:无法读取未定义的属性“顶部” - 滚动错误?

错误未捕获类型错误:无法读取完整日历上未定义的属性“hasTime”

错误“未捕获的类型错误:无法读取未定义的属性‘bannable’”