Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/reactjs/25.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript “反应组件返回”;TypeError:undefined没有属性“;_Javascript_Reactjs - Fatal编程技术网

Javascript “反应组件返回”;TypeError:undefined没有属性“;

Javascript “反应组件返回”;TypeError:undefined没有属性“;,javascript,reactjs,Javascript,Reactjs,我有一个组件,它就在下面,稍后在react中的另一个组件中使用 代码如下: import React, { PropTypes } from 'react'; const MyComponent = () => ( <div> <div className="row"> <div className="col-md-12"> <div className="media"> <

我有一个组件,它就在下面,稍后在react中的另一个组件中使用

代码如下:

import React, { PropTypes } from 'react';

const MyComponent = () => (
  <div>
    <div className="row">
      <div className="col-md-12">
        <div className="media">
          <div className="media-left media-middle">
            <a href="#no"><img className="media-object" src={this.props.image} alt="" /></a>
          </div>
          <div className="media-body"><span className="pull-right">{this.props.name}</span>
            <h4 className="media-heading">{this.props.title}</h4>
            {this.props.desc}
          </div>
        </div>
      </div>
    </div>
  </div>
);

MyComponent.propTypes = {
  image: PropTypes.string,
  name: PropTypes.string,
  title: PropTypes.string,
  desc: PropTypes.string,
};

export default MyComponent;

//Then on other component

import React from 'react';
import MyComponent from './mycomponent.component';

<MyComponent
  title="Title Here"
  name="Some Name"
  desc="Description here"
  image="http://placehold.it/100x100"
/>
import React,{PropTypes}来自'React';
常量MyComponent=()=>(
{this.props.name}
{this.props.title}
{this.props.desc}
);
MyComponent.propTypes={
image:PropTypes.string,
名称:PropTypes.string,
标题:PropTypes.string,
desc:PropTypes.string,
};
导出默认MyComponent;
//然后在其他组件上
从“React”导入React;
从“/MyComponent.component”导入MyComponent;
问题是它没有呈现,开发人员控制台返回以下错误:

“类型错误:未定义没有属性”


这里的问题是什么?我如何解决这个问题?

您必须将道具传递给
MyComponent
,如下所示:

const MyComponent = (props) => (
     console.log("example prop is", props.image);
);

我面临着类似的问题。这是因为初始数组被定义为空数组
[]
。这将使react将其定义为
未定义
,这将导致问题。相反,您可以使用随机元素初始化数组,然后在设置状态时替换它

就我而言,我使用的是-

class App extends React.Component {
  state = {
    userdata:[], /* Notice that the array is empty*/
   };


  responseGoogle = (response) => {
  console.log(response);
  this.setState({userdata: response});
  }
我把它改成了

class App extends React.Component {
  state = {
    userdata:['Hello'], /* Notice that the array has an initial element*/
   };


  responseGoogle = (response) => {
  console.log(response);
  this.setState({userdata: response});
  }

我不确定问题是什么,但使用初始元素解决了问题

您需要在函数中包含
props
参数,并使用
props.foo
而不是
this.props.foo
。它是一个
无状态功能组件
关键字在该函数中不可用,除了需要将
道具
作为
函数
参数接收外,还需要检查
无状态功能组件
上的stackoverflow文档:缺少括号bro!在这里,我想我叛逆的方式会被忽视。已添加缺少的括号。