简体   繁体   English

无法读取属性道具的反应

[英]Cannot read property props react

I have this error where there is an uncaught type error thats says 我有一个未捕获的类型错误,就是说这个错误

Uncaught TypeError: Cannot read property 'name' of undefined
    at TeamList.render (Team-list.jsx:10)
    at ReactCompositeComponent.js:796
    at measureLifeCyclePerf (ReactCompositeComponent.js:75)
    at ReactCompositeComponentWrapper._renderValidatedComponentWithoutOwnerOrContext (ReactCompositeComponent.js:795)
    at ReactCompositeComponentWrapper._renderValidatedComponent (ReactCompositeComponent.js:822)
    at ReactCompositeComponentWrapper.performInitialMount (ReactCompositeComponent.js:362)
    at ReactCompositeComponentWrapper.mountComponent (ReactCompositeComponent.js:258)
    at Object.mountComponent (ReactReconciler.js:46)
    at ReactDOMComponent.mountChildren (ReactMultiChild.js:238)
    at ReactDOMComponent._createInitialChildren (ReactDOMComponent.js:697)

Right now I am working on two files which is App.jsx and Team-list.jsx but I can't determine which one is causing the error. 现在,我正在处理两个文件,即App.jsx和Team-list.jsx,但我无法确定是哪个文件导致了错误。 In my getPlayers method I am returning an array of objects that is used by renderPlayers and rendered by the component. 在我的getPlayers方法中,我返回由renderPlayers使用并由组件呈现的对象数组。 This is happening in the App.jsx 这发生在App.jsx

import React, { Component } from 'react';
import MuiThemeProvider from 'material-ui/styles/MuiThemeProvider';
import RaisedButton from 'material-ui/RaisedButton';
import AppBar from 'material-ui/AppBar';
import { List } from 'material-ui/List';
import Divider from 'material-ui/Divider';

import Player from './Player.jsx';
import TeamList from './Team-list.jsx';
import TeamStats from './Team-stats.jsx';

export default class App extends Component {
  getPlayers(){
    return [
      {
      _id: 1,
      name: "Emmanuel Francisco",
      ballManipulation: 2,
      kickingAbilities: 3,
      passingAbilities: 2,
      duelTackling: 2,
      fieldCoverage: 2,
      blockingAbilities: 2,
      gameStrategy: 2,
      playmakingRisks: 2,
    },
    {
    _id: 2,
    name: "Tinker Tailor",
    ballManipulation: 1,
    kickingAbilities: 1,
    passingAbilities: 1,
    duelTackling: 2,
    fieldCoverage: 2,
    blockingAbilities: 2,
    gameStrategy: 2,
    playmakingRisks: 2,
    },
    {
    _id: 3,
    name: "Soldier Spy",
    ballManipulation: 2,
    kickingAbilities: 3,
    passingAbilities: 2,
    duelTackling: 2,
    fieldCoverage: 2,
    blockingAbilities: 2,
    gameStrategy: 1,
    playmakingRisks: 1,
    },
  ];
}

renderPlayers(){
  return this.getPlayers().map((player) => (
    <TeamList key={player._id} player={player} />
  ));
}

  render(){
    return (
      <MuiThemeProvider>
        <div className="container">
          <AppBar
            title="Soccer Application" iconClassNameRight="muidocs-icon-navigation-expand-more" showMenuIconButton={false} />
            <div className="row">
              <div className="col s12 m7"> <Player /> </div>
              <div className="col s12 m5">
                <Divider/>
                  <List>
                    {this.renderPlayers()}
                  </List>
                <Divider/>
             </div>
              <div className="col s12 m5"> <TeamList /> </div>
            </div>
          </div>
        </MuiThemeProvider>
      )
    }
  }

Then in the Team-list.jsx I try to access the props that I passed in renderPlayers method. 然后,在Team-list.jsx我尝试访问在renderPlayers方法中传递的道具。

import React, { Component } from 'react';
import Avatar from 'material-ui/Avatar';
import { ListItem } from 'material-ui/List';


export default class TeamList extends Component {
  render(){
    return (
        <ListItem
          primaryText={this.props.player.name}
          leftAvatar={<Avatar src="player.jpg" />}
        />
    )
  }
}

Help me determine the cause of the error. 帮我确定错误原因。 Thank you. 谢谢。

The cause of the error is the line 错误的原因是行

       </div>
          <div className="col s12 m5"> <TeamList /> </div>
        </div>

So although you are sending prop in renderPlayers function you are not doing that here. 因此,尽管您要在renderPlayers函数中发送proprenderPlayers此处并未这样做。 So add the props in this line and the error shall go 所以在这行添加道具,错误就会消失

       </div>
          <div className="col s12 m5"> <TeamList player={this.getPlayers[0]}/> </div>
        </div>

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM