Skip to content

Commit 3f9d77b

Browse files
committed
add custome react-redux;add global quick css
1 parent 42cbf00 commit 3f9d77b

File tree

12 files changed

+221
-61
lines changed

12 files changed

+221
-61
lines changed

src/index.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import * as React from 'react'
22
import * as ReactDOM from 'react-dom'
33
import App from './pages/app'
4-
import './assets/scss/app.scss'
4+
import './scss/app.scss'
55
import 'antd/dist/antd.css'
66
import 'quill/dist/quill.snow.css'
77
import serviceWorker from './serviceWorker'

src/pages/demo/demoRedux.tsx

Lines changed: 47 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import * as React from 'react'
22
import { Row, Col, Button, Badge, Icon, Form, Input, Select } from 'antd'
3-
import { ReduxProvider, connect } from '@plugins/react-redux'
4-
import { store } from './reducer'
3+
import { ReduxProvider, connect } from '../../plugins/react-redux'
4+
import { store, countActions } from './reducer'
55

66
console.log(store.getState(), '---store')
77

@@ -24,8 +24,7 @@ const styles = {
2424
}
2525
}
2626

27-
28-
export default class DemoRedux extends React.Component<IProps> {
27+
class DemoRedux extends React.Component<any> {
2928

3029

3130
add = () => {
@@ -36,36 +35,56 @@ export default class DemoRedux extends React.Component<IProps> {
3635
}
3736

3837
shouldComponentUpdate(preState: any, nextProps: any) {
39-
console.log(preState, nextProps)
40-
return false
38+
console.log(preState, nextProps, 'shouldComponentUpdate')
39+
return true
4140
}
4241

4342
componentDidMount() {
44-
console.log(this, 'store.getState().counter')
43+
console.log(this.props, 'demo --- componentDidMount store.getState().counter')
4544
}
4645

4746
render(){
4847
// const count = 9
49-
const { count } = store.getState().counter
50-
console.log( count , ' =-===count ')
48+
const { counter: {count}, disabled } = this.props
49+
const { ADD, DECREASE } = this.props
50+
console.log( count , 'demo render =-===count', disabled, )
5151

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>
52+
return <div>
53+
<section style={styles.block}>
54+
<h3>redux测试</h3>
55+
<Row style={styles.padding}>
56+
<Col span={24}>
57+
<Button onClick={DECREASE}>
58+
<Icon type="minus" />
59+
</Button>
60+
<Badge count={count} showZero={true}/>
61+
<Button onClick={ADD}>
62+
<Icon type="plus" />
63+
</Button>
64+
</Col>
65+
</Row>
66+
</section>
67+
</div>
7068
}
71-
}
69+
}
70+
71+
72+
const DemosComponentWithRedux = connect(
73+
(state: any) => ({
74+
counter: state.counter,
75+
disabled: state.counter.count > 10
76+
}),
77+
(dispatch: Function) => ({
78+
ADD: () => dispatch(countActions.ADD),
79+
DECREASE: () => dispatch(countActions.DECREASE)
80+
})
81+
)(DemoRedux)
82+
83+
84+
const ProviderComponent: React.FC = () => {
85+
return <ReduxProvider store={store}>
86+
<DemosComponentWithRedux/>
87+
</ReduxProvider>
88+
}
89+
90+
export default ProviderComponent

src/pages/demo/reducer.ts

Lines changed: 16 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,4 @@
1-
import { createStore, combineReducers } from '../../plugins/redux'
2-
3-
interface IAction {
4-
type: string
5-
playload: any
6-
}
1+
import { createStore, combineReducers, IAction } from '../../plugins/redux'
72

83
const initCountState = {
94
count: 0
@@ -14,14 +9,27 @@ const initInfoState = {
149
desc: '我们可以学习到redux的原理...'
1510
}
1611

12+
const ADD = 'ADD', DECREASE = 'DECREASE'
13+
14+
export const countActions = {
15+
16+
[ADD]: {
17+
type: ADD
18+
},
19+
20+
[DECREASE]: {
21+
type: DECREASE
22+
}
23+
}
24+
1725
// reducer
1826
const countReducer = (state = initCountState, action: IAction) => {
1927
switch(action.type) {
20-
case 'ADD' :
28+
case ADD :
2129
state.count++
2230
console.log('add', state)
2331
return state
24-
case 'DECREASE' :
32+
case DECREASE :
2533
state.count--
2634
console.log('dec', state)
2735
return state

src/pages/lottery/ballEdit.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -156,7 +156,7 @@ export default class BallEdit extends React.Component<IProps> {
156156
</Row>
157157
<Row>
158158
<Col span={24} offset={3}>
159-
<Button onClick={save} type="primary">保存</Button>
159+
<Button onClick={save} type="primary" className="mgr16">保存</Button>
160160
<Button onClick={this.back}>取消</Button>
161161
</Col>
162162
</Row>

src/plugins/react-redux/connect.tsx

Lines changed: 38 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,55 @@
11
import React, { Component } from 'react'
2+
import PropTypes from 'prop-types'
3+
import { ICreateStore } from '../redux'
24

3-
export function connect<T>(mapStateToProps: Function, mapDispatchToProps: Function) {
5+
export function connect<T>(
6+
mapStateToProps?: Function,
7+
mapDispatchToProps?: Function
8+
) {
49

5-
return (WrappedComponent: React.ComponentType) => {
10+
return function connectWithComponent(WrappedComponent: React.ComponentType) {
611

7-
return class Connect extends Component {
8-
constructor(props: any) {
12+
return class CustomeComponent extends Component {
13+
14+
static contextTypes = {
15+
store: PropTypes.shape({
16+
subscribe: PropTypes.func.isRequired,
17+
dispatch: PropTypes.func.isRequired,
18+
getState: PropTypes.func.isRequired
19+
}).isRequired
20+
}
21+
22+
constructor(props: any, context: {store: ICreateStore}) {
923
super(props)
10-
// this.state =
24+
this.store = context.store
25+
this.state = mapStateToProps(this.store.getState())
26+
if(typeof mapDispatchToProps === 'function') {
27+
this.mappedDispatch = mapDispatchToProps(this.store.dispatch)
28+
}
29+
console.log('connect....constructor', this.state, this.mappedDispatch)
1130
}
31+
32+
state = {}
33+
34+
store = {} as ICreateStore
35+
mappedDispatch: any = {}
36+
unsub = () => {}
1237

1338
componentDidMount() {
14-
//
39+
console.log('connect----componentDidMount', mapStateToProps(this.store.getState()))
40+
this.unsub = this.store.subscribe(() => {
41+
const mappedState = mapStateToProps(this.store.getState())
42+
console.log(mappedState, '----mappedState')
43+
this.setState(mappedState)
44+
})
1545
}
1646

1747
componentWillUnmount() {
18-
//
48+
this.unsub()
1949
}
2050

2151
render() {
22-
return <WrappedComponent {...this.props}/>
52+
return <WrappedComponent {...this.props} {...this.state} {...this.mappedDispatch}/>
2353
}
2454
}
2555
}

src/plugins/react-redux/provider.tsx

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,33 @@
11
import React, { Component } from 'react'
2-
import { IAnyObject } from '../redux/model'
2+
import { IAnyObject, ICreateStore } from '../redux/model'
33
import PropTypes from 'prop-types'
44

55
interface IProviderProps{
6-
$store: IAnyObject
6+
store: IAnyObject
77
}
88

9-
export class ReduxProvider extends Component<IProviderProps> {
9+
export class ReduxProvider extends Component<IProviderProps, ICreateStore> {
1010

1111
static childContextTypes = {
12-
$store: PropTypes.object
12+
store: PropTypes.shape({
13+
subscribe: PropTypes.func.isRequired,
14+
dispatch: PropTypes.func.isRequired,
15+
getState: PropTypes.func.isRequired
16+
}).isRequired
1317
}
1418

1519
constructor(props: IProviderProps) {
1620
super(props)
17-
this.store = props.$store
21+
this.$store = props.store
22+
console.log('ReduxProvider---constructor', 999)
1823
}
1924

20-
store = {}
25+
$store: IAnyObject = {}
2126

2227
getChildContext() {
28+
console.log('获取了---getChildContext', this.$store)
2329
return {
24-
$store: this.store
30+
store: this.$store
2531
}
2632
}
2733

src/plugins/redux/createStore.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,13 +31,13 @@ export const createStore = (reducer: Function, initState?: any, enhancer?: Funct
3131
}
3232
try{
3333
isDispatch = true
34-
console.log('触发方法:', action, '更新state:', JSON.stringify(state))
34+
console.log('触发方法:', action, '更新之前state:', JSON.stringify(state))
3535
state = reducer(state, action)
3636
} finally {
3737
isDispatch = false
3838
}
3939
listeners.forEach(ln => ln())
40-
console.log('更新state之后:', state)
40+
console.log('更新state之后:', JSON.stringify(state))
4141
}
4242

4343
// 更新reducer

src/plugins/redux/index.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
1-
1+
export * from './model'
22
export * from './createStore'
33
export * from './combineReducers'

src/plugins/redux/model.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,8 @@ export interface IAnyObject {
99
}
1010

1111
export interface ICreateStore {
12-
getState: Function
13-
dispatch: Function
14-
subscribe: Function
15-
replaceReducer: Function
12+
getState: () => any
13+
dispatch: (action: IAction) => void
14+
subscribe: (listener: Function) => () => void
15+
replaceReducer: (nextReducer: Function) => void
1616
}

src/assets/scss/app.scss renamed to src/scss/app.scss

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
1+
@import './functions.scss';
12

23
#app {
3-
height: 100%;
4+
// height: 100%;
45
}
56

67
#not-found{

0 commit comments

Comments
 (0)