Reactjs 嵌套路由和redux集成不起作用?
我正在我的项目中嵌套路由。我在App.js中定义了路由,在组件中我有更多的路由,我希望它们被嵌套。唯一的问题是,我的嵌套路由位于连接到redux的组件中。嵌套路由工作不正常 我已经从官方文档中尝试过了,但它不起作用 App.jsReactjs 嵌套路由和redux集成不起作用?,reactjs,react-redux,react-router,react-router-v4,react-router-dom,Reactjs,React Redux,React Router,React Router V4,React Router Dom,我正在我的项目中嵌套路由。我在App.js中定义了路由,在组件中我有更多的路由,我希望它们被嵌套。唯一的问题是,我的嵌套路由位于连接到redux的组件中。嵌套路由工作不正常 我已经从官方文档中尝试过了,但它不起作用 App.js import { connect } from "react-redux"; import { withRouter, Route } from "react-router-dom"; function HowItWorks() { return ( &
import { connect } from "react-redux";
import { withRouter, Route } from "react-router-dom";
function HowItWorks() {
return (
<div>
<h2 style={{ margin: 20 }}>How It Works</h2>
</div>
);
}
function AboutUs() {
return (
<div>
<h2 style={{ margin: 20 }}>About Us</h2>
</div>
);
}
class App extends React.Component {
render() {
return (
<div>
<Route path="/" exact component={HowItWorks} />
<Route path="/howitworks" exact component={HowItWorks} />
<Route path="/aboutus" component={AboutUs} />
<Route path="/admin" component={AdminContainer} />
</div>
);
}
}
MainDesktopComponent.js
我已经尝试过这种方法,即在交换机内部提供嵌套路由和许多不同的方法,但它不起作用。还请注意,我还希望通过mapstatetoprops将道具传递给仪表板组件,该组件将来自上面的redux容器组件
import React from "react";
import Dashboard from "./Dashboard";
import { Route } from "react-router-dom";
import { Switch } from "react-router";
function MainDesktopComponent(props) {
return (
<div>
<Switch>
<Route
exact
path="/admin/dashboard"
render={props => {
<Dashboard/>;
}}
/>
</Switch>
</div>
);
}
export default MainDesktopComponent;
从“React”导入React;
从“/Dashboard”导入仪表板;
从“react router dom”导入{Route};
从“反应路由器”导入{Switch};
功能MainDesktopComponent(道具){
返回(
{
;
}}
/>
);
}
导出默认MainDesktopComponent;
我不确定,但是试试这个怎么样
<Switch>
<Route
exact
path="/admin/dashboard"
render={cProps => <Dashboard {...cProps} {...props} />}
/>
</Switch>
}
/>
返回路由渲染组件。我尝试了这个。但这条路线不仅不起作用。当我转到路径,即admin/dashboard时,它不会加载组件。我在react开发工具中看到。请帮忙@我希望这能帮助你@谢谢你!!它工作得很好。我没有把开关关上!!
<Switch>
<Route
exact
path="/admin/dashboard"
render={cProps => <Dashboard {...cProps} {...props} />}
/>
</Switch>