Предупреждение: React.createElement: ожидал строку (для встроенных компонентов) или класс / функцию (для составных компонентов), но получил: объект
Я новичок в React и столкнулся с какой-то проблемой и получил ошибку: - Предупреждение: React.createElement: тип недействителен - ожидалась строка (для встроенных компонентов) или класс / функция (для составных компонентов), но получил: объект. Ошибка: тип элемента недействителен: ожидается строка (для встроенных компонентов) или класс / функция (для составных компонентов), но получено: объект. Я проверил несколько блогов и реализовал их решения, но все равно получаю ту же ошибку.
index.js
import React from 'react';
import ReactDOM from 'react-dom';
import App from './components/App';
ReactDOM.hydrate(
<App />,
document.getElementById('mountNode'),
);
App.js
import React, { useState } from 'react';
import { Route, Router,Link } from 'react-router-dom';
import { createMemoryHistory } from 'history';
import HomePage from './HomePage';
import About from './About';
export default function App()
{
const history = createMemoryHistory();
return (
<Router history={history}>
{/* A <Switch> looks through its children <Route>s and
renders the first one that matches the current URL. */}
<Route path="/about" component={About} />
<Route path="/" component={HomePage} />
</Router>
);
}
ошибка
Warning: React.createElement: type is invalid -- expected a string (for built-in components) or a
class/function (for composite components) but got: object.
Error: Element type is invalid: expected a string (for built-in components) or a class/function (for
composite components) but got: object.
at ReactDOMServerRenderer.render (C:\LMS-APP\node_modules\react-dom\cjs\react-dom-
server.node.development.js:3743:17)
at ReactDOMServerRenderer.read (C:\LMS-APP\node_modules\react-dom\cjs\react-dom-
server.node.development.js:3373:29)
at Object.renderToString (C:\LMS-APP\node_modules\react-dom\cjs\react-dom-
server.node.development.js:3988:27)
at C:\LMS-APP\src\server\/server.js:10:40
at Layer.handle [as handle_request] (C:\LMS-APP\node_modules\express\lib\router\layer.js:95:5)
at next (C:\LMS-APP\node_modules\express\lib\router\route.js:137:13)
at Route.dispatch (C:\LMS-APP\node_modules\express\lib\router\route.js:112:3)
at Layer.handle [as handle_request] (C:\LMS-APP\node_modules\express\lib\router\layer.js:95:5)
at C:\LMS-APP\node_modules\express\lib\router\index.js:281:22
at Function.process_params (C:\LMS-APP\node_modules\express\lib\router\index.js:335:12)
package.json
{
"name": "LMS-APP",
"version": "1.0.0",
"description": "",
"main": "index.js",
"scripts": {
"dev-server": "nodemon --exec babel-node src/server/server.js --ignore dist/",
"dev-bundle": "webpack -w -d"
},
"keywords": [],
"author": "",
"license": "ISC",
"dependencies": {
"@babel/core": "^7.9.6",
"@babel/node": "^7.8.7",
"@babel/preset-env": "^7.9.6",
"@babel/preset-react": "^7.9.4",
"babel-loader": "^8.1.0",
"express": "^4.17.1",
"react": "^16.13.1",
"react-dom": "^16.13.1",
"react-router-dom": "^5.2.0",
"webpack": "^4.43.0",
"webpack-cli": "^3.3.11"
},
"devDependencies": {
"babel-eslint": "^10.1.0",
"eslint": "^7.0.0",
"eslint-plugin-react": "^7.20.0",
"eslint-plugin-react-hooks": "^4.0.2",
"nodemon": "^2.0.4"
}
}
HomePage.js
import React from 'react-dom';
import {Link} from 'react-router-dom';
export default function HomePage()
{
return(
<div>
<nav>
<ul>
<li>
<Link to="/">Home</Link>
</li>
<li>
<Link to="/about">About</Link>
</li>
</ul>
</nav>
</div>
);
}
server.js
import express from 'express';
import React from 'react';
import ReactDOMServer from 'react-dom/server';
import App from '../components/App';
import StaticRouter from 'react-router-dom';
const server = express();
server.use(express.static('dist'));
server.get('/', (req, res) => {
const initialMarkup = ReactDOMServer.renderToString(<StaticRouter><App /></StaticRouter>);
res.send(`
<html>
<head>
<title>Sample React App</title>
</head>
<body>
<div id="mountNode">${initialMarkup}</div>
<script src="/main.js"></script>
</body>
</html>
`)
});
server.listen(4242, () => console.log('Server is running...'));
About.js
import React from 'react-dom';
export default function About()
{
return(
<div>
<h1>About</h1>
</div>
);
}