前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >React |使用Antd 进行布局学习

React |使用Antd 进行布局学习

作者头像
heidsoft
发布2023-03-18 17:19:41
1K0
发布2023-03-18 17:19:41
举报
  • node : v18.0.0
  • npx: 8.5.2
  • npx create-react-app react-route-layout
  • antd: "^5.1.2"

路由使用:BrowserRouter,源自:react-router-dom

代码语言:javascript
复制
import React from 'react';
import ReactDOM from 'react-dom/client';
import './index.css';
import App from './App';
import reportWebVitals from './reportWebVitals';
import {BrowserRouter} from 'react-router-dom'

const root = ReactDOM.createRoot(document.getElementById('root'));
root.render(
  <React.StrictMode>
    <BrowserRouter>
      <App />
    </BrowserRouter>
  </React.StrictMode>
);

// If you want to start measuring performance in your app, pass a function
// to log results (for example: reportWebVitals(console.log))
// or send to an analytics endpoint. Learn more: https://bit.ly/CRA-vitals
reportWebVitals();

菜单基于:antd,菜单图标源自:@ant-design/icons

代码语言:javascript
复制
import { Menu } from "antd"
//图标
import { HomeOutlined, DashboardOutlined, UnorderedListOutlined, UserOutlined, PoweroffOutlined } from "@ant-design/icons"
import { Routes, Route, useNavigate } from "react-router-dom"
function App() {
  return (
    <div style={{
      display:"flex",
      flexDirection: "column",
      flex:1,
      height:"100vh"
    }}>
      <Headers />
      <div style={{
        display:"flex",
        flexDirection: "row",
        flex:1
      }}>
        <SideMenu />
        <Content />
      </div>
      <Fouter />
    </div>
  );
}

function Headers() {
  return (<div 
    style={{ 
      height: 60, 
      backgroundColor: "lightblue", 
      color: "black", 
      display: 'flex', 
      justifyContent: 'center', 
      alignItems: 'center',
      fontWeight: 'bold'
    }}>Headers</div>);
}

function Fouter() {
  return (<div 
    style={{ 
      height: 60, 
      backgroundColor: "lightgray", 
      color: "white", 
      display: 'flex', 
      justifyContent: 'center', 
      alignItems: 'center',
      fontWeight: 'bold'
    }}>Fouter</div>);
}

function SideMenu() {
  const navigate = useNavigate()
  return (<div style={{ display: 'flex', flexDirection: 'row' }}>
    <Menu
      onClick={({ key }) => {
        if (key === 'signout') {
          // 登出操作
        } else {
          console.log(key)
          navigate(key);
        }
      }}
      defaultSelectedKeys={[window.location.pathname]}
      items={[
        { label: 'Home', key: '/', icon: <HomeOutlined /> },
        { label: "Dashboard", key: '/dashboard', icon: <DashboardOutlined /> },
        { label: "User List", key: '/usersList', icon: <UnorderedListOutlined />,
        children:[
          {label: "活动用户", key: '/activeUsers'},
          {label: "禁用用户", key: '/disableUsers'}
        ]},
        { label: "Profiles", key: '/profile', icon: <UserOutlined /> },
        { label: "Signout", key: 'signout', icon: <PoweroffOutlined />, danger: true }
      ]}>
    </Menu>
  </div>);
}

function Content() {
  return <div>
    <Routes>
      <Route path="/" element={<div>Home</div>}></Route>
      <Route path="/dashboard" element={<div>Dashboard</div>}></Route>
      <Route path="/activeUsers" element={<div>活动 List</div>}></Route>
      <Route path="/disableUsers" element={<div>禁用 List</div>}></Route>
      <Route path="/profile" element={<div>Profiles</div>}></Route>
      <Route path="signout" element={<div>signout</div>}></Route>
    </Routes>
  </div>;
}

export default App;
  1. https://developer.mozilla.org/zh-CN/docs/Web/CSS/CSS_Flexible_Box_Layout/Basic_Concepts_of_Flexbox
  2. https://dummyjson.com/docs/products
  3. https://github.com/heidsoft-frontend/react-route-layout
本文参与 腾讯云自媒体分享计划,分享自微信公众号。
原始发表:2023-01-02,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 云数智圈 微信公众号,前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档