简体   繁体   English

组件未定义 // React 和 Webpack

[英]Component is not defined// React and Webpack

I'm building a React App that will pull from Opendota's api and display the heroes to me.我正在构建一个 React 应用程序,它将从 Opendota 的 api 中提取并向我显示英雄。 This is my first time working with API's.这是我第一次使用 API。 I build the App from scratch, so I didn't use CRA (create-react-app).我从头开始构建应用程序,所以我没有使用 CRA (create-react-app)。

This is my App.js这是我的 App.js

import React from 'react';
import {hot} from "react-hot-loader";
import Heroes from "./components/Heroes"

class App extends Component {
    render() {
        return (
            <Heroes heroes={this.state.heroes} />
        )
    }

    state = {
        heroes: []
    };

    componentDidMount() {
        fetch('https://api.opendota.com/api/heroes')
            .then(res => res.json())
            .then((data) => {
                this.setState({ heroes: data })
            })
            .catch(console.log)
    }
}


export default hot(module)(App);

Heroes.js英雄联盟

import React from 'react'

const Heroes = ({heroes}) => {
    return (
        <div>
            <h1>Hero List</h1>
            {heroes.map((hero) => (
                <div>
                    <div>
                        <h5>{hero.name}</h5>
                        <h6>{hero.id}</h6>

                    </div>
                </div>
            ))}
        </div>
    )
};

export default Heroes

Just looking for an answer that will point me in the right direction.只是在寻找一个可以为我指明正确方向的答案。

You have not imported Component from react.So yo must have to import Component from 'react' .so your code will be looks like你还没有从 react.so 中导入组件,所以你必须从 'react' 中导入组件。所以你的代码看起来像

import React,{Component} from 'react';
import {hot} from "react-hot-loader";
import Heroes from "./components/Heroes"

class App extends Component {
   render() {
      return (
          <Heroes heroes={this.state.heroes} />
      )
   }

   state = {
      heroes: []
   };

   componentDidMount() {
      fetch('https://api.opendota.com/api/heroes')
        .then(res => res.json())
        .then((data) => {
            this.setState({ heroes: data })
        })
        .catch(console.log)
    }
   }


 export default hot(module)(App);

The way to work with component of React使用 React 组件的方式

import React, {Component} from 'react';
class App extends Component{}

other way if you don't import, directly import其他方式如果你不导入,直接导入

import React from 'react';
class App extends React.Component{}

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

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