【发布时间】:2018-06-06 20:48:53
【问题描述】:
想知道是否有一种方法可以基于每个选项卡的嵌套路由来渲染路由。
例如,当我们浏览到
http://localhost:3000/base/a
http://localhost:3000/base/b
我们希望有一个base 组件,它提供某种布局,并基于我们单击的tab 应该呈现nested route 组件。
以下示例不使用 react-router 来渲染选项卡的组件。
下面是路由配置
<Route path={"/base:scope" } exact={true} { component: BaseComponent } />
下面的组件将呈现带有标签的页面
import { Layout, Tabs, Row, Col } from 'antd';
import { Link, NavLink, Route, Switch } from 'react-router-dom';
export class BaseComponent extends React.Component {
public state = {
collapsed: false,
};
private onCollapse = collapsed => {
this.setState({ collapsed });
}
get tabs() {
const { collaborators, integration, settings, configuration, billing, tokens, referrals } = this.props;
return [
{
key: 'a',
tab: 'a',
component: ComponentA,
path: '/base/a',
},
{
key: 'b',
tab: 'b',
component: ComponentB,
path: '/base/b',
},
];
}
public componentWillReceiveProps(props) {
if (!_.isEqual(_.get(this.props, 'match.url'), _.get(props, 'match.url'))) {
this.setState({ key: _.get(props, 'match.url') });
}
}
public render() {
const { renderer, router } = this.context;
const { onLogOut, match, user, profile } = this.props;
return (
<React.Fragment>
<Header className={renderer.renderRule(styles.header, this.props)}>
<div className={renderer.renderRule(styles.title, this.props)}>
Account Settings
</div>
</Header>
<Content>
<div className={renderer.renderRule(styles.container, this.props)}>
<Tabs
animated={false}
defaultActiveKey={match.url}
onChange={key => router.history.push(key)}
className={`${renderer.renderRule(styles.tabs)}`}
>
{_.map(this.tabs, (record, index) => (
<TabPane
key={record.path}
className={renderer.renderRule(styles.pane)}
tab={<span>{record.tab}</span>}
>
{React.createElement(record.component, null, null)}
</TabPane>
))}
</Tabs>
</div>
</Content>
</React.Fragment >
);
}
}
预期:
我们想写成更具体的 react-router
<Routes path={"/base"} exact={false} component={ComponentBase} />
<Routes path={"/base/a"} exact={true} component={ComponentBase} />
<Routes path={"/base/b"} exact={true} component={ComponentBase} />
但是在这种情况下,我们不知道如何渲染页面,因为 react-router 不渲染页面,所以我们注意到选项卡但没有渲染内容。
这是修改后的组件,没有渲染内容,因为我们期望 react-router 渲染组件。
export class BaseComponent extends React.Component {
public state = {
collapsed: false,
};
private onCollapse = collapsed => {
this.setState({ collapsed });
}
get tabs() {
const { collaborators, integration, settings, configuration, billing, tokens, referrals } = this.props;
return [
{
key: 'a',
tab: 'a',
path: '/base/a',
},
{
key: 'b',
tab: 'b',
path: '/base/b',
},
];
}
public componentWillReceiveProps(props) {
if (!_.isEqual(_.get(this.props, 'match.url'), _.get(props, 'match.url'))) {
this.setState({ key: _.get(props, 'match.url') });
}
}
public render() {
const { renderer, router } = this.context;
const { onLogOut, match, user, profile } = this.props;
return (
<React.Fragment>
<Header className={renderer.renderRule(styles.header, this.props)}>
<div className={renderer.renderRule(styles.title, this.props)}>
Account Settings
</div>
</Header>
<Content>
<div className={renderer.renderRule(styles.container, this.props)}>
<Tabs
animated={false}
defaultActiveKey={match.url}
onChange={key => router.history.push(key)}
className={`${renderer.renderRule(styles.tabs)}`}
>
{_.map(this.tabs, (record, index) => (
<TabPane
key={record.path}
className={renderer.renderRule(styles.pane)}
tab={<span>{record.tab}</span>}
>
</TabPane>
))}
</Tabs>
</div>
</Content>
</React.Fragment >
);
}
}
【问题讨论】:
标签: reactjs react-router-v4 antd