【问题标题】:How to render parts in React如何在 React 中渲染部件
【发布时间】:2021-05-10 03:04:45
【问题描述】:

您好,我目前正在做 Spotify 克隆编码。 如果您按侧边栏上的菜单,我希望呈现它旁边的白色背景。 我该怎么做?

  <SideBackground>
            <LogoImg src="https://music-b26f.kxcdn.com/wp-content/uploads/2017/06/635963274692858859903160895_spotify-logo-horizontal-black.jpg"/>

            <Link to="/">
                <Option Icon={HomeIcon} title="Home"></Option>
            </Link>
            <Link to="/search">
                <Option Icon={HomeIcon} title="Search"></Option>
            </Link>
            <Link to="/library">
                <Option Icon={HomeIcon} title="Library"></Option>
            </Link>

            <StringTitle>PLAYLIST</StringTitle>

            <Line />

            {playList.map(({ name }) => (
            <PrintList>{name}</PrintList>
            ))}
            
  </SideBackground>

上面的代码是侧边栏代码。

<Router>
     <Switch>
          <Route exact path="/" component={Main}/>
          <Route exact path="/search" component={Search}/>
          <Route exact path="/library" component={Library} />
     </Switch>
</Router>    

以上代码是app中连接路由器的代码。

目前的代码,部分渲染整个页面是改的,不是这样的。

【问题讨论】:

  • 您希望在右侧渲染组件而不改变或刷新整个页面。然后将 组件设为 const 并放置在 switch case 之前,其余部分使用 Link 用于此用例。此示例可能会解决您的问题:reactrouter.com/web/example/sidebar
  • 我想在不同的文件中编写视图和侧边栏的代码,而不是像示例代码那样将它们写在一个文件中。

标签: reactjs react-router react-router-dom spotify


【解决方案1】:

您可以在 react-router-dom 中使用 sidenav 功能。

import React from "react";
import {
  BrowserRouter as Router,
  Switch,
  Route,
  Link
} from "react-router-dom";

// Each logical "route" has two components, one for
// the sidebar and one for the main area. We want to
// render both of them in different places when the
// path matches the current URL.

// We are going to use this route config in 2
// spots: once for the sidebar and once in the main
// content section. All routes are in the same
// order they would appear in a <Switch>.
const routes = [
  {
    path: "/",
    exact: true,
    sidebar: () => <div>home!</div>,
    main: () => <h2>Home</h2>
  },
  {
    path: "/bubblegum",
    sidebar: () => <div>bubblegum!</div>,
    main: () => <h2>Bubblegum</h2>
  },
  {
    path: "/shoelaces",
    sidebar: () => <div>shoelaces!</div>,
    main: () => <h2>Shoelaces</h2>
  }
];

export default function SidebarExample() {
  return (
    <Router>
      <div style={{ display: "flex" }}>
        <div
          style={{
            padding: "10px",
            width: "40%",
            background: "#f0f0f0"
          }}
        >
          <ul style={{ listStyleType: "none", padding: 0 }}>
            <li>
              <Link to="/">Home</Link>
            </li>
            <li>
              <Link to="/bubblegum">Bubblegum</Link>
            </li>
            <li>
              <Link to="/shoelaces">Shoelaces</Link>
            </li>
          </ul>

          <Switch>
            {routes.map((route, index) => (
              // You can render a <Route> in as many places
              // as you want in your app. It will render along
              // with any other <Route>s that also match the URL.
              // So, a sidebar or breadcrumbs or anything else
              // that requires you to render multiple things
              // in multiple places at the same URL is nothing
              // more than multiple <Route>s.
              <Route
                key={index}
                path={route.path}
                exact={route.exact}
                children={<route.sidebar />}
              />
            ))}
          </Switch>
        </div>

        <div style={{ flex: 1, padding: "10px" }}>
          <Switch>
            {routes.map((route, index) => (
              // Render more <Route>s with the same paths as
              // above, but different components this time.
              <Route
                key={index}
                path={route.path}
                exact={route.exact}
                children={<route.main />}
              />
            ))}
          </Switch>
        </div>
      </div>
    </Router>
  );
}

请阅读react-router-dom-sidenav

【讨论】:

    猜你喜欢
    • 2017-08-12
    • 2023-04-03
    • 2021-06-07
    • 2015-09-07
    • 2021-03-05
    • 2020-06-28
    • 1970-01-01
    • 2017-07-23
    • 2017-02-27
    相关资源
    最近更新 更多