Skip to content

Commit 42cbf00

Browse files
committed
add custom redux plugin
1 parent fd40928 commit 42cbf00

File tree

18 files changed

+342
-13
lines changed

18 files changed

+342
-13
lines changed

build/webpack.base.conf.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -100,6 +100,7 @@ module.exports = {
100100
'@constants': resolve('src/constants'),
101101
'@models': resolve('src/models'),
102102
'@pages': resolve('src/pages'),
103+
'@plugins': resolve('src/plugins'),
103104
'@utils': resolve('src/utils')
104105
}
105106
},

src/pages/demo/demoRedux.tsx

Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
import * as React from 'react'
2+
import { Row, Col, Button, Badge, Icon, Form, Input, Select } from 'antd'
3+
import { ReduxProvider, connect } from '@plugins/react-redux'
4+
import { store } from './reducer'
5+
6+
console.log(store.getState(), '---store')
7+
8+
const FormItem = Form.Item
9+
const TextArea = Input.TextArea
10+
const Option = Select.Option
11+
12+
const styles = {
13+
block: {
14+
marginBottom: '20px',
15+
boxShadow: 'rgba(0, 0, 0, 0.12) 0px 1px 6px, rgba(0, 0, 0, 0.12) 0px 1px 4px',
16+
padding: '10px'
17+
},
18+
flex: {
19+
display: 'flex',
20+
alignItems: 'center'
21+
},
22+
padding: {
23+
padding: '10px 0'
24+
}
25+
}
26+
27+
28+
export default class DemoRedux extends React.Component<IProps> {
29+
30+
31+
add = () => {
32+
store.dispatch({type: 'ADD'})
33+
}
34+
dec = () => {
35+
store.dispatch({type: 'DECREASE'})
36+
}
37+
38+
shouldComponentUpdate(preState: any, nextProps: any) {
39+
console.log(preState, nextProps)
40+
return false
41+
}
42+
43+
componentDidMount() {
44+
console.log(this, 'store.getState().counter')
45+
}
46+
47+
render(){
48+
// const count = 9
49+
const { count } = store.getState().counter
50+
console.log( count , ' =-===count ')
51+
52+
return <ReduxProvider store={store}>
53+
<div>
54+
<section style={styles.block}>
55+
<h3>redux测试</h3>
56+
<Row style={styles.padding}>
57+
<Col span={24}>
58+
<Button onClick={this.dec}>
59+
<Icon type="minus" />
60+
</Button>
61+
<Badge count={count} showZero={true}/>
62+
<Button onClick={this.add}>
63+
<Icon type="plus" />
64+
</Button>
65+
</Col>
66+
</Row>
67+
</section>
68+
</div>
69+
</ReduxProvider>
70+
}
71+
}

src/pages/demo/reducer.ts

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
import { createStore, combineReducers } from '../../plugins/redux'
2+
3+
interface IAction {
4+
type: string
5+
playload: any
6+
}
7+
8+
const initCountState = {
9+
count: 0
10+
}
11+
12+
const initInfoState = {
13+
name: '这里是Redux',
14+
desc: '我们可以学习到redux的原理...'
15+
}
16+
17+
// reducer
18+
const countReducer = (state = initCountState, action: IAction) => {
19+
switch(action.type) {
20+
case 'ADD' :
21+
state.count++
22+
console.log('add', state)
23+
return state
24+
case 'DECREASE' :
25+
state.count--
26+
console.log('dec', state)
27+
return state
28+
default :
29+
return state
30+
}
31+
}
32+
33+
// reducer
34+
const infoReducer = (state = initInfoState, action: IAction) => {
35+
let random = + Math.random()
36+
switch(action.type) {
37+
case 'SET_NAME' :
38+
state.name = '这里是Redux' + random
39+
return state
40+
case 'SET_DESC' :
41+
state.desc = '我们可以学习到redux的原理...' + random
42+
return state
43+
default :
44+
return state
45+
}
46+
}
47+
48+
49+
// 合并reducer
50+
const rootReducer = combineReducers({
51+
counter: countReducer,
52+
info: infoReducer
53+
})
54+
55+
// store
56+
const store = createStore(rootReducer)
57+
58+
export {
59+
store
60+
}

src/components/shared/sider.tsx renamed to src/pages/home/components/sider.tsx

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -50,18 +50,20 @@ const MenuList = [{
5050
key: '99-0', title: '示例', path: '/home/demos', icon:'experiment'
5151
},{
5252
key: '99-1', title: '示例mobx', path: '/home/demo-mobx', icon:'experiment'
53+
},{
54+
key: '99-2', title: '示例redux', path: '/home/demo-redux', icon:'experiment'
5355
}]
5456
}]
5557

5658
class SiderComponent extends React.Component<ISiderProps> {
5759

58-
componentWillReceiveProps() {
59-
// console.log('willReceiveProps')
60-
}
60+
// componentWillReceiveProps() {
61+
// // console.log('willReceiveProps')
62+
// }
6163

62-
componentWillUpdate(props: any){
63-
// console.log('willUpdate')
64-
}
64+
// componentWillUpdate(props: any){
65+
// // console.log('willUpdate')
66+
// }
6567

6668
componentDidMount(){
6769
// console.log('didMount')
@@ -71,6 +73,7 @@ class SiderComponent extends React.Component<ISiderProps> {
7173
const {collapsed} = this.props
7274

7375
return (<Sider
76+
width="260"
7477
trigger={null}
7578
collapsible
7679
collapsed={collapsed}>

src/pages/home/home.tsx

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,10 @@ import * as React from 'react'
22
import { Layout } from 'antd'
33
import { inject, observer } from 'mobx-react'
44
import Routes from '../../routes'
5-
import Sider from '@components/shared/sider'
6-
import Header from '@components/shared/header'
7-
import Footer from '@components/shared/footer'
8-
import { LocaleProvider } from 'antd'
5+
import Sider from './components/sider'
6+
import Header from './components/header'
7+
import Footer from './components/footer'
8+
import { LocaleProvider, ConfigProvider } from 'antd'
99

1010
const { Content } = Layout
1111

@@ -16,7 +16,7 @@ class Home extends React.Component<IProps> {
1616
render(){
1717
const {homeStore} = this.props
1818

19-
return (<LocaleProvider locale={homeStore.lang}>
19+
return (<ConfigProvider locale={homeStore.lang}>
2020
<Layout>
2121
<Sider {...homeStore}/>
2222
<Layout>
@@ -27,7 +27,7 @@ class Home extends React.Component<IProps> {
2727
<Footer/>
2828
</Layout>
2929
</Layout>
30-
</LocaleProvider>)
30+
</ConfigProvider>)
3131
}
3232
}
3333

src/pages/home/store.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,8 @@ import { JWT_TOKEN, SYS_LANG } from '@constants/index'
66
const zhCN: any = zh_CN
77

88
class HomeStore {
9-
@observable authorized: boolean = (storage.get(JWT_TOKEN) || '').split('.').length === 3
9+
@observable authorized: boolean = true
10+
// @observable authorized: boolean = (storage.get(JWT_TOKEN) || '').split('.').length === 3
1011
@observable collapsed: boolean = false
1112
@observable lang: any = storage.get(SYS_LANG) === 'CN' ? zhCN : undefined
1213

src/plugins/react-redux/connect.tsx

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
import React, { Component } from 'react'
2+
3+
export function connect<T>(mapStateToProps: Function, mapDispatchToProps: Function) {
4+
5+
return (WrappedComponent: React.ComponentType) => {
6+
7+
return class Connect extends Component {
8+
constructor(props: any) {
9+
super(props)
10+
// this.state =
11+
}
12+
13+
componentDidMount() {
14+
//
15+
}
16+
17+
componentWillUnmount() {
18+
//
19+
}
20+
21+
render() {
22+
return <WrappedComponent {...this.props}/>
23+
}
24+
}
25+
}
26+
}

src/plugins/react-redux/index.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
export * from './provider'
2+
export * from './connect'

0 commit comments

Comments
 (0)