简体   繁体   English

反应路由器不会在路由更改时渲染组件

[英]react router not rendering components on route change

On initial load of the application it will render the correct component corresponding to the URL However, but when I change click on link, it render nothing.在应用程序的初始加载时,它将呈现与 URL 对应的正确组件但是,但是当我更改单击链接时,它什么也不呈现。

index.tsx:索引.tsx:

import React from 'react';
import { render } from 'react-dom';
import { Provider } from 'react-redux';
import { store } from './store/store';
import { App } from './components/app/App';
import { BrowserRouter } from 'react-router-dom';



render(

    <Provider store={store}>
        <BrowserRouter>
            <App />
        </BrowserRouter>
    </Provider>,
    document.getElementById('app')
);

Mye file app.tsx where I put my routers: This is what I'm intending the routes components to look like. Mye 文件 app.tsx 我放置路由器的位置:这就是我希望路由组件看起来的样子。

import React from 'react';
import { Router, Route, Switch, Redirect, Link } from 'react-router-dom';
import { connect } from 'react-redux';
import {withRouter} from 'react-router-dom';
import { history } from '../../utils/helpers';
import { alertActions } from '../../store/actions'
import { PrivateRoute } from '../../routes';
import { Home, Login } from './components/';

interface AProps {
    alert: {
        message: string,
        type: string
    },
    clearAlerts: () => { type: string }
}

class App extends React.Component<AProps> {
    constructor(props: AProps) {
        super(props);

        history.listen((data) => {
            console.log(data);
            // clear alert on location change
            this.props.clearAlerts();
        });
    }

    render() {
        const { alert } = this.props;
        return (
            <Router history={history}>
                <div className="jumbotron">
                    <div className="container">
                        <div className="col-sm-8 col-sm-offset-2">
                            {alert.message &&
                                <div className={`alert ${alert.type}`}>{alert.message}</div>
                            }
                            <Link to="/login">Login</Link>
                            <Switch>
                                <Route path="/login" component={Login} />
                                {/* <PrivateRoute path="/" component={Home} /> */}
                                {/* <Redirect from="*" to="/" /> */}
                            </Switch>
                        </div>
                    </div>
                </div>
            </Router>
        );
    }
}

function mapState(state: any) {
    const { alert } = state;
    return { alert };
}

const actionCreators = {
    clearAlerts: alertActions.clear
};

const connectedApp = withRouter(connect(mapState, actionCreators)(App));
export { connectedApp as App };

s there something I'm missing to get components to render clicking Link?我缺少一些东西来让组件呈现单击链接吗? I'm not getting any console errors or anything telling me there's an issue.我没有收到任何控制台错误或任何告诉我有问题的信息。 So not sure if components are not wrapped correctly or what may be causing the issue.所以不确定组件是否未正确包装或可能导致问题的原因。

Change this:改变这个:

<Route path="/login" component={Login} />

to this:对此:

<Route exact path="/login" component={Login} />

You are using 2 Routers at the same time.您同时使用 2 个路由器。 One inside index file, another inside app file.一个内部index文件,另一个内部app文件。 You might want to remove one of them.您可能想要删除其中之一。

For example, only leave one inside index :例如,只留下一个内部index

<Router history={history}>
    <App />
</Router>

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

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