1- import { useState } from "react" ;
1+ import { useCallback , useState } from "react" ;
22import MyIcon from "../icon" ;
33import { Button , Drawer , message , Row , Radio } from "antd" ;
4- import { connect } from "react-redux" ;
5- import { changeLayoutMode } from "@/store/layout/action" ;
6- import { setVisible } from "@/store/visibel/action" ;
7- import * as Types from "../../store/layout/actionTypes" ;
4+ import { useDispatch , useSelector } from "react-redux" ;
5+ import { setVisible as stateSetVisibel , changeLayoutMode } from "@/store/action" ;
6+ import * as Types from "@/store/layout/actionTypes" ;
87import { setLayoutMode , setCompVisibel as util_setCompVisibel } from "@/utils" ;
98import singImg from "@/assets/images/layout2.jpg" ;
109import twoImg from "@/assets/images/layout1.jpg" ;
1110import twoFlanksImg from "@/assets/images/layout3.jpg" ;
11+ import { getComponentsVisible , getLayoutMode } from "@/store/getters" ;
1212import "./index.less" ;
13+
1314const modes = [
1415 {
1516 img : singImg ,
@@ -37,41 +38,27 @@ const RadioArray = [
3738 v : false ,
3839 } ,
3940] ;
40- const mapDispatchToProps = ( dispatch ) => ( {
41- setMode : ( mode ) => dispatch ( changeLayoutMode ( mode ) ) ,
42- setCompVisible : ( key , val ) => dispatch ( setVisible ( key , val ) ) ,
43- } ) ;
44- const mapStateToProps = ( state ) => ( {
45- layoutMode : state . layout ,
46- componentsVisible : state . componentsVisible ,
47- } ) ;
48- function useLayoutSet ( { setMode, layoutMode, componentsVisible } ) {
41+
42+ export default function LayoutSet ( ) {
4943 const [ visible , setVisible ] = useState ( false ) ;
50- const wakeup = ( ) => setVisible ( true ) ;
51- const onClose = ( ) => setVisible ( false ) ;
52- const setLayout = ( mode ) => {
53- setMode ( mode ) ;
54- message . success ( "布局设置成功!" ) ;
55- } ;
56- const saveLayout = ( ) => {
44+ // state
45+ const dispatch = useDispatch ( )
46+ const componentsVisible = useSelector ( getComponentsVisible )
47+ const layoutMode = useSelector ( getLayoutMode )
48+
49+ const setMode = useCallback ( ( mode ) => dispatch ( changeLayoutMode ( mode ) ) , [ dispatch ] )
50+ const wakeup = useCallback ( ( ) => setVisible ( true ) , [ ] ) ;
51+ const onClose = useCallback ( ( ) => setVisible ( false ) , [ ] ) ;
52+ const saveLayout = useCallback ( ( ) => {
5753 setLayoutMode ( layoutMode ) ;
5854 util_setCompVisibel ( componentsVisible ) ;
5955 message . success ( "布局保存本地成功!" ) ;
60- } ;
61-
62- return {
63- wakeup,
64- visible,
65- onClose,
66- setLayout,
67- saveLayout,
68- } ;
69- }
56+ } , [ layoutMode , componentsVisible ] ) ;
57+ const setLayout = useCallback ( ( mode ) => {
58+ setMode ( mode ) ;
59+ message . success ( "布局设置成功!" ) ;
60+ } , [ setMode ] ) ;
7061
71- function LayoutSet ( props ) {
72- const { wakeup, visible, onClose, setLayout, saveLayout } =
73- useLayoutSet ( props ) ;
74- const { componentsVisible = { } , setCompVisible } = props ;
7562 return (
7663 < div className = "layoutset-container" >
7764 < MyIcon type = "icon_setting" onClick = { wakeup } />
@@ -90,7 +77,7 @@ function LayoutSet(props) {
9077 < div
9178 key = { m . mode }
9279 onClick = { ( ) => setLayout ( m . mode ) }
93- className = { m . mode === props . layoutMode ? "col active" : "col" }
80+ className = { m . mode === layoutMode ? "col active" : "col" }
9481 >
9582 < img src = { m . img } alt = { m . alt } > </ img >
9683 </ div >
@@ -101,7 +88,7 @@ function LayoutSet(props) {
10188 < Row key = { key } className = "visibel-list" >
10289 { key === "footer" ? "底部:" : "顶部切换导航:" }
10390 < Radio . Group
104- onChange = { ( e ) => setCompVisible ( key , e . target . value ) }
91+ onChange = { ( e ) => dispatch ( stateSetVisibel ( key , e . target . value ) ) }
10592 value = { componentsVisible [ key ] }
10693 >
10794 { RadioArray . map ( ( i ) => (
@@ -121,5 +108,3 @@ function LayoutSet(props) {
121108 </ div >
122109 ) ;
123110}
124-
125- export default connect ( mapStateToProps , mapDispatchToProps ) ( LayoutSet ) ;
0 commit comments