简体   繁体   English

React.js:元素类型无效:

[英]React.js: Element type is invalid:

I am creating a sample react.js project in VS 2019我正在 VS 2019 中创建一个示例 react.js 项目

My Index.js我的 Index.js

import 'bootstrap/dist/css/bootstrap.css';
import React from 'react';
import ReactDOM from 'react-dom';
import { Provider } from 'react-redux';
import { ConnectedRouter } from 'react-router-redux';
import { createBrowserHistory } from 'history';
import configureStore from './store/configureStore';
import App from './App';
import registerServiceWorker from './registerServiceWorker';

// Create browser history to use in the Redux store
const baseUrl = document.getElementsByTagName('base')[0].getAttribute('href');
const history = createBrowserHistory({ basename: baseUrl });

// Get the application-wide store instance, prepopulating with state from the server where available.
const initialState = window.initialReduxState;
const store = configureStore(history, initialState);

const rootElement = document.getElementById('root');

ReactDOM.render(
  <Provider store={store}>
    <ConnectedRouter history={history}>
      <App />
    </ConnectedRouter>
  </Provider>,
  rootElement);

registerServiceWorker();

My App.js我的 App.js

import React, { Component } from 'react'
import NavBar from './components/NavBar'
import CoursesList from './components/CourseList'


export default class App extends React.Component {

    render() {
        return (
            <div>
                <NavBar />
                <CoursesList />
            </div>
        )
    }
}

On running I am getting error:运行时出现错误:

Element type is invalid: expected a string (for built-in components) or a class/function (for composite components) but got: object.元素类型无效:应为字符串(对于内置组件)或类/函数(对于复合组件)但得到:对象。

Check the render method of NavBar .检查NavBar的渲染方法。

I am using Material UI我正在使用 Material UI

NavBar.js导航栏.js

import React from 'react'
import AppBar from '@material-ui/core/AppBar'
import Toolbar from '@material-ui/core/Toolbar'
import Typography from '@material-ui/core/Typography'
const NavBar = () => {
    return (
        <div>
            <AppBar position="static">
                <Toolbar>
                    <Typography variant="title" color="inherit">
                        React & Material-UI Sample Application
                </Typography>
                </Toolbar>
            </AppBar>
        </div>
    )
}
export default NavBar;

PAckage.json包.json

"name": "React_Material_UI",
"version": "0.1.0",
"private": true,
"dependencies": {
  "bootstrap": "^4.3.1",
  "jquery": "3.3.1",
  "react": "^16.0.0",
  "react-dom": "^16.0.0",
  "react-redux": "^5.0.6",
  "react-router-bootstrap": "^0.24.4",
  "react-router-dom": "^4.2.2",
  "react-router-redux": "^5.0.0-alpha.8",
  "react-scripts": "^1.1.5",
  "reactstrap": "^6.3.0",
  "redux": "^3.7.2",
  "redux-thunk": "^2.2.0",
  "rimraf": "^2.6.2",
},
"devDependencies": {
  "ajv": "^6.0.0",
  "babel-eslint": "^7.2.3",
  "cross-env": "^5.2.0",
  "eslint": "^4.1.1",
  "eslint-config-react-app": "^2.1.0",
  "eslint-plugin-flowtype": "^2.50.3",
  "eslint-plugin-import": "^2.14.0",
  "eslint-plugin-jsx-a11y": "^5.1.1",
  "eslint-plugin-react": "^7.11.1"
},
"eslintConfig": {
  "extends": "react-app"
},
"scripts": {
  "start": "rimraf ./build && react-scripts start",
  "build": "react-scripts build",
  "test": "cross-env CI=true react-scripts test --env=jsdom",
  "eject": "react-scripts eject",
  "lint": "eslint ./src/"
}

Any clue?有什么线索吗?

Try to import NavBar like this in MyApp.js尝试在MyApp.js像这样导入NavBar

import React, { Component } from 'react'
import { NavBar } from './components/NavBar'
import CoursesList from './components/CourseList'


export default class App extends React.Component {

    render() {
        return (
            <div>
                <NavBar />
                <CoursesList />
            </div>
        )
    }
}

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

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