我正在使用最新版本的create-react-app。我搜索了所有教程并尝试了一些不同教程的网站,但我没有收到错误。
我正在尝试在我的应用中返回页面。我已经完成了功能组件和基于类的组件,但没有看。我试过包装作为回报,但它不会渲染。所有应用程序都有效但页面内容不显示:(
About.js和Home.js是这样的:
import React from 'react';
const About = () => (
<div>
<h2>About</h2>
</div>
);
export default About;
Nav.js很简单:
import React from 'react';
import { BrowserRouter as Router, Route, NavLink } from "react-router-dom";
import './css/Nav.css';
const Nav = () => (
<ul>
<li>
<NavLink
activeClassName="active"
to="/">Home</NavLink>
</li>
<li>
<NavLink
activeClassName="active"
to="/about">Admin</NavLink>
</li>
</ul>
);
export default Nav;
最后是index.js
import React, { Component } from 'react';
import ReactDOM from 'react-dom';
import {
BrowserRouter as Router,
Route,
Switch
} from "react-router-dom";
import registerServiceWorker from './misc';
// COMPONENTS
import Nav from './nav/Nav';
// PAGE COMPONENTS
import Home from './pages/Home';
import About from './pages/About';
import Page404 from './pages/404';
// CSS
import './css/Index.css';
// CONSTRUCT
class SmartApartmentApp extends Component {
render() {
return (
<Router>
<div className="App">
<Nav />
<Switch>
<Route exact path="/" Component={Home} />
<Route path="/about" Component={About} />
<Route component={Page404} />
</Switch>
<Footer />
</div>
</Router>
);
}
}
// EXECUTE
export default SmartApartmentApp;
ReactDOM.render(<SmartApartmentApp />, document.getElementById('root'));
registerServiceWorker()
;
答案 0 :(得分:1)
使用错误的道具名称的这个问题!
Route
代码中的使用component
道具名称代替Component
例如
<Route path="/about" Component={About} />
应该是:
<Route path="/about" component={About} />
答案 1 :(得分:1)
使用组件属性而不是组件。