我正在为我的新Web应用程序使用Reaction路由V6.我正在使用带有嵌套路由的插座来换出<main>个组件,这工作得很好.现在,我在主组件的左侧还有一个左侧面板.在那个侧面板中,有一个顶部的按钮,我也想根据路由交换.我怎样才能正确地做到这一点呢?我知道你不能有一个以上的门店.

以下是我的主要布局:

return (
    <div className={'d-flex flex-column h-100'}>
        <header className={'border-bottom'}>
            <h4>Title</h4>
        </header>
        <div className={'d-flex align-items-stretch h-100'}>
            <LeftPane /> <!-- second part to be swapped would be in this component -->
            <main className={'p-2'}>
                <Outlet />
            </main>
        </div>
        <footer className={'py-2 border-top'}>
            <div className={'d-flex flex-row w-50 mx-auto justify-content-between'}>
                <div>&copy; {year}</div>
                <div>company</div>
                <div>address</div>
            </div>
        </footer>
    </div>
);

我的路由代码非常简单:

<Router>
    <Routes>
        <Route path={'/'} element={<HomePage />} />
        <Route path={'/dashboard'} element={<DashboardPage />}>
            <Route path={'ingredients'} element={<Ingredients />} />
            <Route path={'users'} element={<Users />} />
        </Route>
    </Routes>
</Router>

我正在交换不同的元素到main段的基础上 Select 的路由.例如,当点击Ingredients时,它会使用OutletIngredients部分切换到<main>部分.

我的Ingredients组件如下所示:

import React from 'react';

const Ingredients = () => {
    return (
        <div className={'d-flex'}>
            <h5 className={'title flex-grow-1 border-bottom text-start pb-1'}>
                Active Ingredients
            </h5>
        </div>
    );
};

export default Ingredients;

And here is a screenshot to show what I am trying to achieve: enter image description here

推荐答案

您可以继续将"公共用户界面"抽象为布局布线组件的模式.

主布局

const 主布局 = () => (
  <div className={'d-flex flex-column h-100'}>
    <header className={'border-bottom'}>
      <h4>Title</h4>
    </header>
    <div className={'d-flex align-items-stretch h-100'}>
      <Outlet />
    </div>
    <footer className={'py-2 border-top'}>
      <div className={'d-flex flex-row w-50 mx-auto justify-content-between'}>
        <div>&copy; {year}</div>
        <div>company</div>
        <div>address</div>
      </div>
    </footer>
  </div>
);

左面板布局

const 左面板布局 = ({ leftPane = <LeftPane /> }) => (
  <>
    {leftPane}
    <main className={'p-2'}>
      <Outlet />
    </main>
  </>
);

将路由渲染到适当的布局路由中.示例:

<Router>
  <Routes>
    <Route element={<主布局 />}>
      <Route path="/" element={<HomePage />} />
      <Route path="/dashboard" element={<DashboardPage />}>
        {/* Defaults to LeftPane component */}
        <Route element={<左面板布局 />}>
          <Route path="ingredients" element={<Ingredients />} />
        </Route>

        {/* Use another leftPane component */}
        <Route element={<左面板布局 leftPane={<SomeOtherPane />} />}>
          <Route path="users" element={<Users />} />
        </Route>
      </Route>
    </Route>
  </Routes>
</Router>

Javascript相关问答推荐

如何在非独立组件中使用独立组件?

用户单击仅在JavaScript中传递一次以及其他行为

通过在页面上滚动来移动滚动条

我开始使用/url?q=使用Cheerio

格式值未保存在redux持久切片中

在JavaScript中声明自定义内置元素不起作用

为什么我的列表直到下一次提交才更新值/onChange

CheckBox作为Vue3中的一个组件

如何调用名称在字符串中的实例方法?

无法从NextJS组件传递函数作为参数'

使用GraphQL查询Uniswap的ETH价格

Angular 订阅部分相互依赖并返回数组多个异步Http调用

Reaction Native中的范围滑块

JS:XML insertBefore插入元素

如何在不影响隐式类型的情况下将类型分配给对象?

在数组中查找重叠对象并仅返回那些重叠对象

如果一个字符串前面有点、空格或无字符串,后面有空格、连字符或无字符串,则匹配正则表达式

将嵌套数组的元素乘以其深度,输出一个和

在ChartJS中使用spanGaps时,是否获取空值的坐标?

如何调整下拉内容,使其不与其他元素重叠?