gpt4 book ai didi

reactjs - react : Loadable is not working?

转载 作者:行者123 更新时间:2023-12-04 15:59:32 25 4
gpt4 key购买 nike

你好我是 React 的新手,我想用 Loadable 实现 routing,但是它不起作用它在 http://localhost:3000/user 时显示空白页 http://localhost:3000/

请你指正我做错的地方。我也越来越-

警告:失败的 prop 类型:提供给 Routestring 类型的无效 prop component,预期的 function.

我的代码是:

home.js

import React, { Component } from 'react';
import { Link} from 'react-router-dom';


class Home extends Component {
render() {
return (
<div>
<h1>Welcome to the Tornadoes Website!</h1>
<h5><Link to="/user">User</Link></h5>
</div>
);
}
}

export default Home;

user.js:

import React, { Component } from 'react';
import { Link } from 'react-router-dom';

class User extends Component {
render() {
return (
<div>
<ul>
<li>6/5 @ Evergreens</li>
<li>6/8 vs Kickers</li>
<li>6/14 @ United</li>
<li><Link to="/">Home</Link></li>
</ul>
</div>
)
}
}
export default User;

App.js:

import React from 'react';
import { Router, Route, Switch } from 'react-router-dom';
import { history } from './helpers/history';
import Loadable from 'react-loadable';
import './App.css';

const Loading = () => <div> Loading... </div>;

const Home = Loadable({
loader: () => import('./components/home-component/home'),
loading: Loading
});

const User = Loadable({
loader: () => import('./components/user-component/user'),
loading: Loading
});

class App extends React.Component {
render() {
return (
<Router history={history}>
<Switch>
<Route exact path="/" component="Home" />
<Route path="/user" component="User" />
</Switch>
</Router>
);
}
}

export default App;

index.js:

import React from 'react';
import ReactDOM from 'react-dom';
import './index.css';
import App from './App';
import registerServiceWorker from './registerServiceWorker';

import { BrowserRouter } from 'react-router-dom'
ReactDOM.render((
<BrowserRouter>
<App />
</BrowserRouter>
), document.getElementById('root'))

registerServiceWorker();

最佳答案

我看到你正在这样做:<Route exact path="/" component="Home" />应该是 <Route exact path="/" component={Home} />既然你要用那个变量,那么当他不知道你要哪个Component的时候,就不可能通过String来引用了。希望对您有所帮助

关于reactjs - react : Loadable is not working?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50905580/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com