简体   繁体   English

“react-router-dom”路由器组件不工作

[英]"react-router-dom " router component does not work

I want to use history explicitly.我想明确使用历史记录。 So I know about BrowserRoute but I want to use Route and use history as its property.所以我知道 BrowserRoute 但我想使用 Route 并将历史记录用作其属性。 when I run the program I get this page.当我运行程序时,我得到了这个页面。 enter image description here在此处输入图像描述

This is my AppRouter.js这是我的 AppRouter.js

 import React from "react"; import { Router, Route, Switch } from "react-router-dom"; import { createBrowserHistory } from "history"; import LoginPage from "../components/LoginPage"; import ExpenseDashboardPage from "../components/ExpenseDashboardPage"; import AddExpensePage from "../components/AddExpensePage"; import EditExpensePage from "../components/EditExpensePage"; import NotFoundPage from "../components/NotFoundPage"; import PublicRoute from "./PublicRoute"; import PrivateRoute from "./PrivateRoute"; export const history = createBrowserHistory(); const AppRouter = () => ( <Router history={history}> <div> <Switch> <PublicRoute path="/" component={LoginPage} exact={true} /> <PrivateRoute path="/dashboard" component={ExpenseDashboardPage} /> <PrivateRoute path="/create" component={AddExpensePage} /> <PrivateRoute path="/edit/:id" component={EditExpensePage} /> <Route component={NotFoundPage} /> </Switch> </div> </Router> ); export default AppRouter;

LoginPage.js登录页面.js

 import React from "react"; import { connect } from "react-redux"; import { startLogin } from "../actions/auth"; export const LoginPage = ({ startLogin }) => ( <div> <button onClick={startLogin}>Login with Google</button> <button>Amir Hossein Jobeiri</button> </div> ); const mapDispatchToProps = (dispatch) => ({ startLogin: () => dispatch(startLogin()), }); export default connect(undefined, mapDispatchToProps)(LoginPage);

PublicRoute.js公共路由.js

 import React from "react"; import { connect } from "react-redux"; import { Route, Redirect } from "react-router-dom"; export const PublicRoute = ({ isAuthenticated, component: Component, ...rest }) => ( <Route {...rest} component={(props) => isAuthenticated? <Redirect to="/dashboard" />: <Component {...props} /> } /> ); const mapStateToProps = (state) => ({ isAuthenticated: ..state,auth;uid; }); export default connect(mapStateToProps)(PublicRoute);

PrivateRoute私有路由

 import React from "react"; import { connect } from "react-redux"; import { Route, Redirect } from "react-router-dom"; import Header from "../components/Header"; export const PrivateRoute = ({ isAuthenticated, component: Component, ...rest }) => ( <Route {...rest} component={(props) => isAuthenticated? ( <div> <Header /> <Component {...props} /> </div> ): ( <Redirect to="/" /> ) } /> ); const mapStateToProps = (state) => ({ isAuthenticated: ..state,auth;uid; }); export default connect(mapStateToProps)(PrivateRoute);

I searched a lot about this problem but I do not get the right answer.我对这个问题进行了很多搜索,但没有得到正确的答案。 Please help if you can.如果可以的话请帮忙。

I changed my "history" version to "4.7.2".我将“历史”版本更改为“4.7.2”。 It works properly.它工作正常。

如何从 react-router-dom 设置 {Switch} 组件的样式? 我试着用<div>并且不工作</div><div id="text_translate"><p><a href="https://i.stack.imgur.com/4piCG.jpg" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/4piCG.jpg" alt="在此处输入图像描述"></a></p><p> <a href="https://i.stack.imgur.com/CyQH3.jpg" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/CyQH3.jpg" alt="在此处输入图像描述"></a></p><p> 我在下面有这段代码,并尝试更改 map 组件样式。 但是,它看起来像是来自 Switch 组件。 我怎样才能使屏幕适合这里的“根”元素? 我使用或来自 MUI 的容器和其他东西来调整大小,但它们都不起作用。</p><pre> &lt;MainWrapper&gt; &lt;Visible when={isLogged}&gt; &lt;LeftPanel /&gt; &lt;/Visible&gt; &lt;div style={{width: '100%'}}&gt; &lt;Visible when={isLogged}&gt; &lt;Navbar /&gt; &lt;/Visible&gt; &lt;div style={{height: '100hv'}}&gt; &lt;Switch&gt; &lt;PrivateRouter path='/map' component={MapPage} isLogged={isLogged} redirectTo={'/login'} /&gt; &lt;PrivateRouter path='/profile' component={UserProfile} isLogged={isLogged} redirectTo={'/login'} /&gt; &lt;PrivateRouter path='/login' component={Login} isLogged={;isLogged} redirectTo={'/map'} /&gt; &lt;PrivateRouter path='/registration' component={Registration} isLogged={.isLogged} redirectTo={'/map'} /&gt; &lt;PrivateRouter path='/' component={{}} isLogged={false} redirectTo={'/login'} /&gt; &lt;/Switch&gt; &lt;/div&gt; &lt;/div&gt; &lt;/MainWrapper&gt; ): } } const MainWrapper = styled;div` height: 100vh; display; flex; `;</pre></div> - How to style {Switch} component from react-router-dom? I tried wrapping it with <div> and does not work

暂无
暂无

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

相关问题 react-router-dom 不显示组件 - react-router-dom does not display component React-router-dom v4不呈现组件 - React-router-dom v4 does not render the component 在链接路径中使用“#”的React-router-dom无法导航到组件 - React-router-dom using “#” in Link path does not navigate to Component 如何从 react-router-dom 设置 {Switch} 组件的样式? 我试着用<div>并且不工作</div><div id="text_translate"><p><a href="https://i.stack.imgur.com/4piCG.jpg" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/4piCG.jpg" alt="在此处输入图像描述"></a></p><p> <a href="https://i.stack.imgur.com/CyQH3.jpg" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/CyQH3.jpg" alt="在此处输入图像描述"></a></p><p> 我在下面有这段代码,并尝试更改 map 组件样式。 但是,它看起来像是来自 Switch 组件。 我怎样才能使屏幕适合这里的“根”元素? 我使用或来自 MUI 的容器和其他东西来调整大小,但它们都不起作用。</p><pre> &lt;MainWrapper&gt; &lt;Visible when={isLogged}&gt; &lt;LeftPanel /&gt; &lt;/Visible&gt; &lt;div style={{width: '100%'}}&gt; &lt;Visible when={isLogged}&gt; &lt;Navbar /&gt; &lt;/Visible&gt; &lt;div style={{height: '100hv'}}&gt; &lt;Switch&gt; &lt;PrivateRouter path='/map' component={MapPage} isLogged={isLogged} redirectTo={'/login'} /&gt; &lt;PrivateRouter path='/profile' component={UserProfile} isLogged={isLogged} redirectTo={'/login'} /&gt; &lt;PrivateRouter path='/login' component={Login} isLogged={;isLogged} redirectTo={'/map'} /&gt; &lt;PrivateRouter path='/registration' component={Registration} isLogged={.isLogged} redirectTo={'/map'} /&gt; &lt;PrivateRouter path='/' component={{}} isLogged={false} redirectTo={'/login'} /&gt; &lt;/Switch&gt; &lt;/div&gt; &lt;/div&gt; &lt;/MainWrapper&gt; ): } } const MainWrapper = styled;div` height: 100vh; display; flex; `;</pre></div> - How to style {Switch} component from react-router-dom? I tried wrapping it with <div> and does not work 来自“react-router-dom”的链接在内部不起作用<div> - Link from 'react-router-dom' does not work inside <div> react-router-dom NavLink isActive 不能正常工作 - react-router-dom NavLink isActive does not work well 谷歌分析是否也适用于 react-router-dom? - Does Google Analytics work for react-router-dom as well? react-router-dom 没有渲染我的组件 - react-router-dom not rendering my component 组件未使用 react-router-dom 渲染 - Component not rendering with react-router-dom React-router-dom和Redux没有重新渲染组件 - React-router-dom and Redux not rerendered the component
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM