React:可装载不起作用?

时间:2018-06-18 08:40:24

标签: reactjs react-router react-redux react-loadable

您好我是新人,我希望routingLoadable一起实施,但是当它 http://localhost:3000/user 或<时它无法正常显示空白页强> http://localhost:3000/

请你纠正我在哪里做错了。 我也得到了 -

警告:道具类型失败:提供给component的{​​{1}}类型的道具string无效,预计为Route

我的代码是:

home.js

function

user.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;

App.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;

index.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;

2 个答案:

答案 0 :(得分:2)

我看到你这样做:<Route exact path="/" component="Home" />应该是<Route exact path="/" component={Home} />,因为你想要使用那个变量,当他不知道你想要哪个Component时,就不可能用String引用。我希望这有帮助

答案 1 :(得分:0)

这让我觉得在调用组件时有一个isRequired propType。你也可以在这里发布你的组件吗?