最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

react如何實現(xiàn)側(cè)邊欄聯(lián)動頭部導(dǎo)航欄效果

 更新時間:2023年03月12日 14:23:17   作者:sky_blue6  
這篇文章主要介紹了react如何實現(xiàn)側(cè)邊欄聯(lián)動頭部導(dǎo)航欄效果,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教

項目中使用react+antd design+redux+react-reouter-dom

實現(xiàn)思路

編寫路由表=》循環(huán)遍歷路由表=》渲染側(cè)邊欄=》單擊側(cè)邊欄獲取相應(yīng)的標簽數(shù)據(jù)=》存入redux=》遍歷循環(huán)redux數(shù)據(jù)渲染頭部導(dǎo)航欄

路由表

const RouterTree = [
? ? {
? ? ? ? key: 'num_one',
? ? ? ? title: {
? ? ? ? ? ? icon: '',
? ? ? ? ? ? text: 'text'
? ? ? ? },
? ? ? ? children: [
? ? ? ? ? ? {
? ? ? ? ? ? ? ? key: '1',
? ? ? ? ? ? ? ? text: 'options1',
? ? ? ? ? ? ? ? path: '/option1',
? ? ? ? ? ? ? ? component: '',
? ? ? ? ? ? ? ? isOutSide: true,
? ? ? ? ? ? }
? ? ? ? ]

? ? },
? ? {
? ? ? ? key: 'num_two',
? ? ? ? title: {
? ? ? ? ? ? icon: '',
? ? ? ? ? ? text: 'text'
? ? ? ? },
? ? ? ? children: [
? ? ? ? ? ? {
? ? ? ? ? ? ? ? key: '2',
? ? ? ? ? ? ? ? text: 'text',
? ? ? ? ? ? ? ? path: '/option1',
? ? ? ? ? ? ? ? component: '',
? ? ? ? ? ? ? ? isOutSide: false
? ? ? ? ? ? }
? ? ? ? ]

? ? },
? ? {
? ? ? ? key: 'num_three',
? ? ? ? title: {
? ? ? ? ? ? icon: '',
? ? ? ? ? ? text: 'text'
? ? ? ? },
? ? ? ? children: [
? ? ? ? ? ? {
? ? ? ? ? ? ? ? key: '3',
? ? ? ? ? ? ? ? text: 'text',
? ? ? ? ? ? ? ? path: '/option1',
? ? ? ? ? ? ? ? component: '',
? ? ? ? ? ? ? ? isOutSide: false
? ? ? ? ? ? }
? ? ? ? ]

? ? },
]
export default RouterTree

側(cè)邊欄渲染

引入路由表,循環(huán)遍歷渲染側(cè)邊欄

import React, { Component } from 'react'
import { Menu } from 'antd';
import { NavLink as Link, withRouter } from 'react-router-dom'
import RouterTree from '@/modules/route.js'

?render() {
? ? ? ? const { activityKey } = this.state || {}
? ? ? ? return (
? ? ? ? ? ? <div>
? ? ? ? ? ? ? ? <Menu
? ? ? ? ? ? ? ? ? ? onClick={this.handleClick}
? ? ? ? ? ? ? ? ? ? defaultSelectedKeys={['1']}
? ? ? ? ? ? ? ? ? ? defaultOpenKeys={['sub1']}
? ? ? ? ? ? ? ? ? ? selectedKeys={activityKey}
? ? ? ? ? ? ? ? ? ? mode="inline"
? ? ? ? ? ? ? ? ? ? inlineCollapsed={isToggle}
? ? ? ? ? ? ? ? >
? ? ? ? ? ? ? ? ? ? {RouterTree.map((item, index) => {
? ? ? ? ? ? ? ? ? ? ? ? return (
? ? ? ? ? ? ? ? ? ? ? ? ? ? <SubMenu
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? key={item.key}
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? title={item.title.text}
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? index={index}
? ? ? ? ? ? ? ? ? ? ? ? ? ? >
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? {item.children && item.children.map((menuItem, menuIndex) => {
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? return (<Menu.Item
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? key={menuItem.key}
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? index={menuIndex}
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? >
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? {menuItem.isOutSide ? <a  rel="noopener noreferrer">baidu</a> : <Link to={menuItem.path}>{menuItem.text}</Link>}
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? </Menu.Item>)
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? })}
? ? ? ? ? ? ? ? ? ? ? ? ? ? </SubMenu>)
? ? ? ? ? ? ? ? ? ? })}
? ? ? ? ? ? ? ? </Menu>
? ? ? ? ? ? </div>
? ? ? ? )

由于單獨使用redux不便,項目中引用react-redux,redux-thunk。

這里將側(cè)邊欄拆分為兩個文件,一個是容器組件,一個為UI組件(以上的為UI組件)

容器組件(側(cè)邊欄)

容器組件為導(dǎo)出的組件,在容器組件中引入UI組件slide

import { connect } from 'react-redux'
import { business } from '@/tools/index.js'
import Slide from './slide'

const { processedArr } = business

const mapStoreToProps = (state) => {
? ? const { dynamicJump,activityKey} = state || {}
? ? let responseRoute = processedArr(dynamicJump)
? ? return {
? ? ? ? dynamicJump: [...responseRoute],
? ? ? ? activityKey:activityKey
? ? }
}

const mapDispatchToProps = (dispatch) => {
? ? return {
? ? ? ? updateRouter(item) {
? ? ? ? ? ? dispatch((dispatch) => {
? ? ? ? ? ? ? ? dispatch({
? ? ? ? ? ? ? ? ? ? type: 'UPDATED_ROUTING', item: { ...item }
? ? ? ? ? ? ? ? })
? ? ? ? ? ? })
? ? ? ? },
? ? ? ? activityKeys(key){
? ? ? ? ? ? dispatch((dispatch)=>{
? ? ? ? ? ? ? ? dispatch({
? ? ? ? ? ? ? ? ? ? type:'ACTIVITY_KEY',key:key
? ? ? ? ? ? ? ? })
? ? ? ? ? ? })
? ? ? ? }
? ? }
}

export default connect(mapStoreToProps, mapDispatchToProps)(Slide)

store

在src下新建一個store文件

import { createStore, applyMiddleware } from 'redux'
import thunk from 'redux-thunk';
import { routerMiddleware } from 'react-router-redux'

let createHistory = require('history').createBrowserHistory
let history = createHistory()
let routerWare = routerMiddleware(history)

const dynamicJump = {
? ? dynamicJump: '這里設(shè)置默認值'(一般存放在session中),
? ? activityKey: '這里設(shè)置默認值',
}

const reducers = function (state = dynamicJump, action) {
? ? switch (action.type) {
? ? ? ? case 'UPDATED_ROUTING':
? ? ? ? ? ? state.dynamicJump =action.item
? ? ? ? ? ? break;
? ? ? ? case 'ACTIVITY_KEY':
? ? ? ? ? ? if (action.key !== undefined) {
? ? ? ? ? ? ? ? state.activityKey = action.key
? ? ? ? ? ? }
? ? ? ? ? ? break;
? ? ? ? default:
? ? ? ? ? ? break;
? ? }
? ? return { ...state }
}

const store = createStore(reducers, applyMiddleware(thunk, routerWare))

// 訂閱事件
store.subscribe(() => {
? ? let state = store.getState();
? ? sessionStorage.setItem('dynamicJump',JSON.stringify(state.dynamicJump))
? ? sessionStorage.setItem('activityKey',JSON.stringify(state.activityKey))
})

export default store

在根目錄下將其注入進去(注意文件的引入位置)

import {Provider} from 'react-redux'
import store from './store/index.js'

ReactDOM.render(
? <Provider store={store}><App /></Provider>,
? document.getElementById('root')
);

頭部導(dǎo)航欄

容器組件

import { connect } from 'react-redux'
import { push } from 'react-router-redux'
import { business } from '@/tools/index.js'
import Header from './head.jsx'

const { processedArr } = business

const mapStoreToProps = (state) => {
? ? const { dynamicJump, activityKey } = state
? ? let newRouteArr = processedArr(dynamicJump)
? ? return {
? ? ? ? dynamicJump: [...newRouteArr],
? ? ? ? activityKey: activityKey
? ? }
}

const mapDispatchToProps = (dispatch) => {
? ? return {
? ? ? ? removeKey(itemKey) {
? ? ? ? ? ? dispatch((dispatch, getStore) => {
? ? ? ? ? ? ? ? const { dynamicJump } = getStore() || {}
? ? ? ? ? ? ? ? let removeArr = processedArr(dynamicJump)
? ? ? ? ? ? ? ? const indexes = removeArr.findIndex((item) => item.key === itemKey)
? ? ? ? ? ? ? ? // 點擊頭部導(dǎo)航條的刪除(即刪除保存在redux的數(shù)組元素即可)
? ? ? ? ? ? ? ? if (removeArr.length > 1) {
? ? ? ? ? ? ? ? ? ? removeArr.splice(indexes, 1)
? ? ? ? ? ? ? ? }
? ? ? ? ? ? ? ? // 刪除之后跳轉(zhuǎn)到刪除元素的前一個元素的路由地址
? ? ? ? ? ? ? ? let path = removeArr[indexes - 1] ? removeArr[indexes - 1].path : '/option1'
? ? ? ? ? ? ? ? let keys = removeArr[indexes - 1] ? removeArr[indexes - 1].key : 0
? ? ? ? ? ? ? ? if(keys===0){
? ? ? ? ? ? ? ? ? ? keys=removeArr[0].key
? ? ? ? ? ? ? ? }
? ? ? ? ? ? ? ? dispatch({
? ? ? ? ? ? ? ? ? ? type: 'UPDATED_ROUTING', item: { ...removeArr },
? ? ? ? ? ? ? ? })
? ? ? ? ? ? ? ? dispatch({
? ? ? ? ? ? ? ? ? ? type: 'ACTIVITY_KEY', key: keys
? ? ? ? ? ? ? ? })
? ? ? ? ? ? ? ? // 這里進行跳轉(zhuǎn)
? ? ? ? ? ? ? ? dispatch(push(path))
? ? ? ? ? ? })
? ? ? ? },
? ? ? ? changeActiveKey(key) {
? ? ? ? ? ? dispatch((dispatch) => {
? ? ? ? ? ? ? ? dispatch({
? ? ? ? ? ? ? ? ? ? type: 'ACTIVITY_KEY', key: key
? ? ? ? ? ? ? ? })
? ? ? ? ? ? })
? ? ? ? }
? ? }
}


export default connect(mapStoreToProps, mapDispatchToProps)(Header)

這里存在著:如果刪除頭部組件的其中一個導(dǎo)航,那么需要跳轉(zhuǎn)到該導(dǎo)航(刪除的)的前一個路由地址,而這個操作在UI組件中是不方便的,這里我采取的方法是引入“history”模塊(詳細代碼請移步到store.js),然后在容器組件中使用dispatch(push(path))跳轉(zhuǎn),也可以使用更為簡單的方法,直接在容器組件中引入react-router的withRouter,包裹在最外層即:export default withRouter(connect(mapStoreToProps, mapDispatchToProps)(Header))

UI組件

render(){
? ?const { dynamicJump } = this.props || {}
? ?const { activeKey } = this.state || {}
?? ?<Tabs
?? ? ? ? ? ? hideAdd
?? ? ? ? ? ? onChange={this.onChange}
?? ? ? ? ? ? activeKey={activeKey}
?? ? ? ? ? ? type="line"
?? ? ? ? ? ? onEdit={this.onEdit}
?? ? ? ? ? ? className='tabs'
?? ? >
?? ? ? ? ? ? ? ? ?{ dynamicJump.map((item) => (
?? ? ? ? ? ? ? ? ? ? ? ? ? ?<TabPane tab={<Link to={item.path}>{item.text}</Link>} key={item.key} />
?? ? ? ? ? ? ? ? ? ?))}
?? ?</Tabs>
}

對于key值,可以使用react新增的鉤子函數(shù):getDerivedStateFromProps進行判斷是否更行

static getDerivedStateFromProps(nextProps, preState) {
? ? ? ? const { activityKey, dynamicJump } = nextProps || {}
? ? ? ? const { activeKey } = preState || {}
? ? ? ? if (activityKey !== activeKey) {
? ? ? ? ? ? return {
? ? ? ? ? ? ? ? activeKey: activityKey
? ? ? ? ? ? }
? ? ? ? }
? ? ? ? return { allRoute: dynamicJump }
?}
?
?remove = targetKey => {
? ? ? ? this.removeKey(targetKey)
? ?}
? removeKey(key) {
? ? ? ? this.props.removeKey(key)
? ? }

總結(jié)

以上為個人經(jīng)驗,希望能給大家一個參考,也希望大家多多支持腳本之家。

相關(guān)文章

  • React中的Context應(yīng)用場景分析

    React中的Context應(yīng)用場景分析

    這篇文章主要介紹了React中的Context應(yīng)用場景分析,Context 提供了一種在組件之間共享數(shù)據(jù)的方式,而不必顯式地通過組件樹的逐層傳遞 props,通過實例代碼給大家介紹使用步驟,感興趣的朋友跟隨小編一起看看吧
    2021-06-06
  • React Fragment介紹與使用詳解

    React Fragment介紹與使用詳解

    本文主要介紹了React Fragment介紹與使用詳解,文中通過示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2021-11-11
  • 解決React報錯Parameter 'props' implicitly has an 'any' type

    解決React報錯Parameter 'props' implicitly&nb

    這篇文章主要為大家介紹了React報錯Parameter 'props' implicitly has an 'any' type的解決處理方法,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪
    2022-12-12
  • 詳解Webpack+Babel+React開發(fā)環(huán)境的搭建的方法步驟

    詳解Webpack+Babel+React開發(fā)環(huán)境的搭建的方法步驟

    本篇文章主要介紹了詳解Webpack+Babel+React開發(fā)環(huán)境的搭建的方法步驟,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2018-01-01
  • React組件化學(xué)習入門教程講解

    React組件化學(xué)習入門教程講解

    React是現(xiàn)在前端使用頻率最高的三大框架之一,React率先提出虛擬DOM的思想和實現(xiàn),使其保持有良好的性能。本篇文章將對React組件化的入門學(xué)習進行講解,同時針對模塊化的思想進行概述,為接下來組件化開發(fā)的文章進行知識儲備
    2022-09-09
  • React學(xué)習之事件綁定的幾種方法對比

    React學(xué)習之事件綁定的幾種方法對比

    這篇文章主要給大家介紹了關(guān)于React學(xué)習之事件綁定的幾種方法對比,文中通過示例代碼介紹的非常詳細,對大家的學(xué)習或者工作具有一定的參考學(xué)習價值,需要的朋友們下面隨著小編來一起學(xué)習學(xué)習吧。
    2017-09-09
  • React中條件渲染的常見方法總結(jié)

    React中條件渲染的常見方法總結(jié)

    條件渲染在React開發(fā)中非常重要的功能,它允許開發(fā)人員根據(jù)條件控制渲染的內(nèi)容,在創(chuàng)建動態(tài)和交互式用戶界面方面發(fā)揮著至關(guān)重要的作用,本文總結(jié)了常用的的條件渲染方法,需要的朋友可以參考下
    2024-01-01
  • react?express實現(xiàn)webssh?demo解析

    react?express實現(xiàn)webssh?demo解析

    這篇文章主要為大家介紹了詳解react?express實現(xiàn)webssh?demo解析,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪
    2023-04-04
  • 詳解在React中跨組件分發(fā)狀態(tài)的三種方法

    詳解在React中跨組件分發(fā)狀態(tài)的三種方法

    這篇文章主要介紹了詳解在React中跨組件分發(fā)狀態(tài)的三種方法,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧
    2018-08-08
  • react封裝Dialog彈框的方法

    react封裝Dialog彈框的方法

    這篇文章主要為大家詳細介紹了react封裝Dialog彈框的方法,文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2022-08-08

最新評論

行唐县| 新巴尔虎右旗| 溧水县| 乐陵市| 德钦县| 巍山| 乌苏市| 大安市| 临西县| 麦盖提县| 砚山县| 荣成市| 青浦区| 东乌| 茂名市| 苍梧县| 集安市| 西和县| 广汉市| 永和县| 垫江县| 太和县| 达孜县| 桦甸市| 自贡市| 江城| 武夷山市| 迁西县| 汝州市| 黔西| 齐齐哈尔市| 丰顺县| 宣武区| 新田县| 和静县| 清水河县| 酒泉市| 新泰市| 隆子县| 依安县| 仙居县|