简体   繁体   English

映射函数错误:无法读取Reactjs中未定义的属性“ map”

[英]Map function error: Cannot read property 'map' of undefined in Reactjs

import React, {Component} from 'react';
import PropTypes from 'prop-types';
import endoskopi from '../Images/endoskopi.jpg';
import '../Css/diagnosis.css'
import {Route,Switch,BrowserRouter,Link} from 'react-router-dom'
import ChosenDiagnosis from "./ChosenDiagnosis";
import App from "./App";


class Diagnosis extends Component {
  constructor(props) {
    super(props);
}


static propTypes = {
    diagnosis: PropTypes.array
};


render() {
    return (
        <BrowserRouter>
            <div>
                <Switch>
                    <Route path='/diagnosis' component={App}/>
                    <Route path='/diagnosis/:id' component={ChosenDiagnosis}/>
                </Switch>

                <div className="row">
                    {( this.props.diagnosis || [] ).map(diagnosis =>{
                        return (<div key={diagnosis.id} className="col-md-4" style={{marginTop:"2rem"}}>
                            <div className="diagnosis-box">
                                <img className="diagnosis-img" src={endoskopi} alt={diagnosis.name}/>
                                <div className="diagnosis-text">{diagnosis.name}</div>
                                <Link to={`/diagnosis/${diagnosis.id}`}>
                                    <button className={"diagnosis-button"}>Ücreti Sorgula</button>
                                </Link>
                            </div>
                        </div>)
                    })}
                </div>
            </div>
        </BrowserRouter>

    );
  }
}

export default Diagnosis;

Hello , I am a newbie at Reactjs . 您好,我是Reactjs的新手。 I got this error but I do not understand the reason because my ' diagnosis ' array has been declared in app.js . 我收到此错误,但我不明白原因,因为在app.js中声明了“ 诊断 ”数组。 Also, I put ' Switch ' block to under the all codes but it did not work. 另外,我将' Switch '块放到所有代码下,但是它不起作用。 May you help me please? 你能帮我吗?

Thank you in advance. 先感谢您。

class App extends Component {
constructor(props) {
    super(props);
}

state={
  diagnosis: [
      {
          name: "EKG",
          id: 1
      },
      {
          name: "Ultrason",
          id: 2
      },
      {
          name: "MR",
          id: 3
      },
      {
          name: "Röntgen",
          id: 4
      },
      {
          name: "EEG",
          id: 5
      },
      {
          name: "EMG",
          id: 6
      },
      {
          name: "Kan Testi",
          id: 7
      },
      {
          name: "İdrar Testi",
          id: 8
      },
      {
          name: "Tomografi",
          id: 9
      },
      {
          name: "Endoskopi",
          id: 10
      },
  ]
};


render() {
return (
  <div className="App">
    <Navbar/>
    <Diagnosis diagnosis={this.state.diagnosis}/>
  </div>
);

} } }}

export default App;

It's also my another component which is App.js. 这也是我的另一个组件App.js。 (Edited by request) (根据要求编辑)

With this static data, your code should work actually. 使用此静态数据,您的代码应该可以实际工作。 You don't have to use any conditional rendering here. 您不必在这里使用任何条件渲染。 But the error in your question indicates that when your component renders the first time there isn't any diagnosis prop at that time. 但是您问题中的错误表明,当您的组件首次渲染时,那时没有任何diagnosis道具。 We should examine this. 我们应该检查一下。

One other problem is in your route definition. 另一个问题是您的路线定义。 Probably you want to do something like that: 可能您想做这样的事情:

<Switch>
    <Route path='/diagnosis' component={Diagnosis}/>
    <Route path='/diagnosis/:id' component={ChosenDiagnosis}/>
</Switch>

Look this part: path='/diagnosis/:id' . 看这部分: path='/diagnosis/:id'

To guarantee let's try this in your code with the fix above: 为了保证让我们在上面的修复程序中尝试使用此代码:

{( this.props.diagnosis || [] ).map(diagnosis => { ....

try to change this part: 尝试更改此部分:

           <Switch>
                <Route path='/diagnosis' component={Diagnosis}/>
                <Route path='/diagnosis:id' component={ChosenDiagnosis}/>
            </Switch>

to: 至:

           <Switch>
                <Route path='/diagnosis' render={() => <Diagnosis diagnosis={this.props.diagnosis}/>}/>
                <Route path='/diagnosis:id' component={ChosenDiagnosis}/>
            </Switch>

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

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