简体   繁体   English

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

[英]Uncaught TypeError: Cannot read property '_props' of undefined

I am trying to pass some data from state, as props to another component as follows:我正在尝试将 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
  }
}

Getting an error Uncaught TypeError: Cannot read property '_props' of undefined .收到错误Uncaught TypeError: Cannot read property '_props' of undefined

Since it's a class you need to return something in the render:由于它是 class 您需要在渲染中返回一些内容:

render() {
  return (
    <Overview ...
  );
}

Is there something loading this class too, passing props into it?是否也有加载这个 class 的东西,将道具传递给它?

First of all make your state object variables starting letter lowercase that is首先让你的 state object 变量开始字母小写

this.state = {data: null}

Second your handleClick has async logic so make use of async and await.其次,您的 handleClick 具有异步逻辑,因此请使用异步和等待。 Like this -像这样 -

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

EDIT: And as @rrd mentioned you need to return the jsx inside render编辑:正如@rrd 提到的,您需要在渲染中返回 jsx

render{
  return(
     ....
 )
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 未捕获的类型错误:无法读取未定义的属性“道具” - Uncaught TypeError: Cannot read property 'props' of undefined 未捕获的TypeError:无法读取undefined(reactjs)的属性&#39;props&#39;(非常简单) - Uncaught TypeError: Cannot read property 'props' of undefined (reactjs) (very simpel) 未捕获到的TypeError:无法读取reactJS中未定义的属性&#39;props&#39; - Uncaught TypeError: Cannot read property 'props' of undefined in reactJS React Redux - Uncaught(in promise)TypeError:无法读取未定义的属性&#39;props&#39; - React Redux - Uncaught (in promise) TypeError: Cannot read property 'props' of undefined 未捕获的TypeError:无法读取react-redux中未定义的属性&#39;props&#39; - Uncaught TypeError: Cannot read property 'props' of undefined in react-redux 道具未捕获类型错误:无法读取未定义的属性“名称” - Props Uncaught TypeError: Cannot read property 'name' of undefined Uncaught TypeError:无法读取handleChange处未定义的属性“ setState”,无法读取handleSubmit处为null的属性“ props” - Uncaught TypeError: Cannot read property 'setState' of undefined at handleChange , Cannot read property 'props' of null at handleSubmit 未捕获的TypeError:无法读取null的属性'props' - Uncaught TypeError: Cannot read property 'props' of null 未捕获的TypeError:无法读取未定义的属性“未定义” - Uncaught TypeError: Cannot read property 'undefined' of undefined 类型错误:无法读取未定义 reactjs 的属性“道具” - TypeError: Cannot read property 'props' of undefined reactjs
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM