Optimizing performance with useMemo:

import React, { Suspense, useMemo } from 'react'
import { Switch, Route } from 'react-router-dom'
import { connect } from 'react-redux'
import Loading from '.. /.. /.. /components/light/Loading'
import Header from './Header'
import Nav from './Nav'

function List(props) {
  const { router } = props
 

  // The listDom is re-rendered only when the router changes. The useMemo function can be used to optimize performance
  const listDom = useMemo(() = > {
    const listDom = []
    const listRender = (arr) = > {
      arr.forEach((item) = > {
        if (item.children) {
          listRender(item.children)
        } else {
          listDom.push(
            <Route
              key={item.path}
              path={item.path}
              component={item.component}
            ></Route>
          )
        }
      })
    }
    listRender(router.toJS())
    return listDom
  }, [router])

  return (
    <div className="m-list">
      <Header></Header>
      <div className="m-list-info">
        <Nav></Nav>
        <Suspense fallback={<Loading isLazyLoading={true}></Loading>} ><Switch>{listDom}</Switch>
        </Suspense>
      </div>
    </div>)}const mapStateToProps = (state) = > {
  return {
    router: state.getIn(['light'.'router']),
    theme: state.getIn(['light'.'theme']),}}const mapDispatchToProps = (dispatch) = > {
  return {
    onSetState(key, value) {
      dispatch({ type: 'SET_LIGHT_STATE', key, value })
    },
    onDispatch(action) {
      dispatch(action)
    },
  }
}

export default connect(mapStateToProps, mapDispatchToProps)(List)
Copy the code

UseEffect can also be used to optimize performance:

import React, { Suspense, useEffect, useState} from 'react'
import { Switch, Route } from 'react-router-dom'
import { connect } from 'react-redux'
import Loading from '.. /.. /.. /components/light/Loading'
import Header from './Header'
import Nav from './Nav'

function List(props) {
  const [listDom, setListDom] = useState([])
  const { router } = props


  // Only when the router changes will the listDom be re-rendered. UseEffect is used to optimize performance
  useEffect(() = > {
    const listDom = []
    const listRender = (arr) = > {
      arr.forEach((item) = > {
        if (item.children) {
          listRender(item.children)
        } else {
          listDom.push(
            <Route
              key={item.path}
              path={item.path}
              component={item.component}
            ></Route>
          )
        }
      })
    }
    listRender(router.toJS())
    setListDom(listDom)
  }, [router])


  return (
    <div className="m-list">
      <Header></Header>
      <div className="m-list-info">
        <Nav></Nav>
        <Suspense fallback={<Loading isLazyLoading={true}></Loading>} ><Switch>{listDom}</Switch>
        </Suspense>
      </div>
    </div>)}const mapStateToProps = (state) = > {
  return {
    router: state.getIn(['light'.'router']),
    theme: state.getIn(['light'.'theme']),}}const mapDispatchToProps = (dispatch) = > {
  return {
    onSetState(key, value) {
      dispatch({ type: 'SET_LIGHT_STATE', key, value })
    },
    onDispatch(action) {
      dispatch(action)
    },
  }
}

export default connect(mapStateToProps, mapDispatchToProps)(List)
Copy the code