React Router 嵌套路由不呈现
React Router nested routes not rendering
我正在尝试以这种方式呈现 Nav 组件以及任何页面内容,因为我希望能够在 Nav 中访问 this.props.location
(以突出显示导航栏上的活动位置),因此已分配它到“/”路由并传递子路由,而不是简单地渲染它。但是,只有 Nav 组件呈现; none 来自其他路由的组件在任何页面上呈现,因为 {this.props.children}
似乎在导航中未定义。我是 React 的新手,所以任何帮助都会很棒。
App.tsx:
const landingPage = () => {
if (Auth.isUserAuthenticated()) {
return (
<UserProfile/>
);
} else {
return (
<Landing />
);
}
};
const routes = () => {
return (
<Route path="/" component={Nav}>
<Route path="/" component={landingPage}/>
<Route path="/login" component={LoginForm}/>
<Route path="/register" component={RegistrationForm}/>
<Route path="/logout" component={Logout}/>
<Route path="/about" component={About}/>
</Route>
)
}
class App extends React.Component<{}, null> {
render() {
return (
<BrowserRouter>
{routes()}
</BrowserRouter>
);
}
}
Nav.tsx
class Nav extends React.Component<any, any> {
constructor() {
super();
};
linkActive = (link) => {
return this.props.location.pathname.includes(link);
};
logInOut = () => {
if (!Auth.isUserAuthenticated()) {
return (
[
<NavItem active={this.linkActive("login")} href="/login">Login</NavItem>,
<NavItem active={this.linkActive("register")} href="/register">Register</NavItem>
]
);
} else {
return (
<NavItem eventKey={"logout"} href="/logout">Logout</NavItem>
);
}
};
render() {
return (
<div>
<Navbar className="fluid collapseOnSelect">
<Navbar.Collapse>
<Navbar.Header>
<Navbar.Brand>
<a href="/">Home</a>
</Navbar.Brand>
<Navbar.Toggle />
</Navbar.Header>
<BootstrapNav>
<NavItem active={this.linkActive("about")} href="/about">About</NavItem>
</BootstrapNav>
<BootstrapNav pullRight>
{this.logInOut()}
</BootstrapNav>
</Navbar.Collapse>
</Navbar>
{this.props.children}
</div>
);
}
}
Nav.tsx 组件
如果我没记错的话你用的是react-router v4
I want to be able to access this.props.location in Nav (to highlight
active location on Nav bar), so have assigned it to "/" route and
passing child routes rather than simply rendering it.
为了能够访问 this.props.location
、this.props.history
或 this.props.match
,请使用 withRouter
高阶组件。
首先让我们导入withRouter
HOC。
import { withRouter } from 'react-router-dom';
要使用它,请用 withRouter
包装您的 Nav
组件。
// example code
export default withRouter(Nav);
App.tsx 组件
重新组织你的路线。我强烈建议使用 Switch
.
// We are still using BrowserRouter
import {
BrowserRouter as Router,
Switch,
} from 'react-router-dom';
<Router>
<Switch>
<Route path="/" component={landingPage}/>
<Route path="/login" component={LoginForm}/>
<Route path="/register" component={RegistrationForm}/>
<Route path="/logout" component={Logout}/>
<Route path="/about" component={About}/>
</Switch>
</Router>
在您的 App.tsx
渲染方法中,您可以在顶部添加导航。
让我们假装只有经过身份验证的用户才能看到导航组件。在您的情况下,您不需要访问 this.props.children
。
renderNav() {
return (this.props.authenticated) ? <Nav /> : null;
}
render() {
return (
<div>
{this.renderNav()}
<Router>
<Switch>
<Route path="/" component={landingPage}/>
<Route path="/login" component={LoginForm}/>
<Route path="/register" component={RegistrationForm}/>
<Route path="/logout" component={Logout}/>
<Route path="/about" component={About}/>
</Switch>
</Router>
</div>
);
}
如果您想了解有关 react-router v4
的更多信息,请阅读 react-training 上的这些示例、指南和 API。希望对你有帮助!
我正在尝试以这种方式呈现 Nav 组件以及任何页面内容,因为我希望能够在 Nav 中访问 this.props.location
(以突出显示导航栏上的活动位置),因此已分配它到“/”路由并传递子路由,而不是简单地渲染它。但是,只有 Nav 组件呈现; none 来自其他路由的组件在任何页面上呈现,因为 {this.props.children}
似乎在导航中未定义。我是 React 的新手,所以任何帮助都会很棒。
App.tsx:
const landingPage = () => {
if (Auth.isUserAuthenticated()) {
return (
<UserProfile/>
);
} else {
return (
<Landing />
);
}
};
const routes = () => {
return (
<Route path="/" component={Nav}>
<Route path="/" component={landingPage}/>
<Route path="/login" component={LoginForm}/>
<Route path="/register" component={RegistrationForm}/>
<Route path="/logout" component={Logout}/>
<Route path="/about" component={About}/>
</Route>
)
}
class App extends React.Component<{}, null> {
render() {
return (
<BrowserRouter>
{routes()}
</BrowserRouter>
);
}
}
Nav.tsx
class Nav extends React.Component<any, any> {
constructor() {
super();
};
linkActive = (link) => {
return this.props.location.pathname.includes(link);
};
logInOut = () => {
if (!Auth.isUserAuthenticated()) {
return (
[
<NavItem active={this.linkActive("login")} href="/login">Login</NavItem>,
<NavItem active={this.linkActive("register")} href="/register">Register</NavItem>
]
);
} else {
return (
<NavItem eventKey={"logout"} href="/logout">Logout</NavItem>
);
}
};
render() {
return (
<div>
<Navbar className="fluid collapseOnSelect">
<Navbar.Collapse>
<Navbar.Header>
<Navbar.Brand>
<a href="/">Home</a>
</Navbar.Brand>
<Navbar.Toggle />
</Navbar.Header>
<BootstrapNav>
<NavItem active={this.linkActive("about")} href="/about">About</NavItem>
</BootstrapNav>
<BootstrapNav pullRight>
{this.logInOut()}
</BootstrapNav>
</Navbar.Collapse>
</Navbar>
{this.props.children}
</div>
);
}
}
Nav.tsx 组件
如果我没记错的话你用的是react-router v4
I want to be able to access this.props.location in Nav (to highlight active location on Nav bar), so have assigned it to "/" route and passing child routes rather than simply rendering it.
为了能够访问 this.props.location
、this.props.history
或 this.props.match
,请使用 withRouter
高阶组件。
首先让我们导入withRouter
HOC。
import { withRouter } from 'react-router-dom';
要使用它,请用 withRouter
包装您的 Nav
组件。
// example code
export default withRouter(Nav);
App.tsx 组件
重新组织你的路线。我强烈建议使用 Switch
.
// We are still using BrowserRouter
import {
BrowserRouter as Router,
Switch,
} from 'react-router-dom';
<Router>
<Switch>
<Route path="/" component={landingPage}/>
<Route path="/login" component={LoginForm}/>
<Route path="/register" component={RegistrationForm}/>
<Route path="/logout" component={Logout}/>
<Route path="/about" component={About}/>
</Switch>
</Router>
在您的 App.tsx
渲染方法中,您可以在顶部添加导航。
让我们假装只有经过身份验证的用户才能看到导航组件。在您的情况下,您不需要访问 this.props.children
。
renderNav() {
return (this.props.authenticated) ? <Nav /> : null;
}
render() {
return (
<div>
{this.renderNav()}
<Router>
<Switch>
<Route path="/" component={landingPage}/>
<Route path="/login" component={LoginForm}/>
<Route path="/register" component={RegistrationForm}/>
<Route path="/logout" component={Logout}/>
<Route path="/about" component={About}/>
</Switch>
</Router>
</div>
);
}
如果您想了解有关 react-router v4
的更多信息,请阅读 react-training 上的这些示例、指南和 API。希望对你有帮助!