11import React , { Component } from 'react'
22import PropTypes from 'prop-types'
33import { ICreateStore } from '../redux'
4+ import { } from './provider'
45
56export function connect < T > (
67 mapStateToProps ?: Function ,
@@ -53,4 +54,58 @@ export function connect<T>(
5354 }
5455 }
5556 }
56- }
57+ }
58+
59+
60+ export function connect1 < T > (
61+ mapStateToProps ?: Function ,
62+ mapDispatchToProps ?: Function
63+ ) {
64+
65+ return function connectWithComponent ( WrappedComponent : React . ComponentType ) {
66+
67+ return class CustomeComponent extends Component {
68+
69+ static contextTypes = {
70+ store : PropTypes . shape ( {
71+ subscribe : PropTypes . func . isRequired ,
72+ dispatch : PropTypes . func . isRequired ,
73+ getState : PropTypes . func . isRequired
74+ } ) . isRequired
75+ }
76+
77+ constructor ( props : any , context : { store : ICreateStore } ) {
78+ super ( props )
79+ this . store = context . store
80+ this . state = mapStateToProps ( this . store . getState ( ) )
81+ if ( typeof mapDispatchToProps === 'function' ) {
82+ this . mappedDispatch = mapDispatchToProps ( this . store . dispatch )
83+ }
84+ console . log ( 'connect....constructor' , this . state , this . mappedDispatch )
85+ }
86+
87+ state = { }
88+
89+ store = { } as ICreateStore
90+ mappedDispatch : any = { }
91+ unsub = ( ) => { }
92+
93+ componentDidMount ( ) {
94+ console . log ( 'connect----componentDidMount' , mapStateToProps ( this . store . getState ( ) ) )
95+ this . unsub = this . store . subscribe ( ( ) => {
96+ const mappedState = mapStateToProps ( this . store . getState ( ) )
97+ console . log ( mappedState , '----mappedState' )
98+ this . setState ( mappedState )
99+ } )
100+ }
101+
102+ componentWillUnmount ( ) {
103+ this . unsub ( )
104+ }
105+
106+ render ( ) {
107+ return < WrappedComponent { ...this . props } { ...this . state } { ...this . mappedDispatch } />
108+ }
109+ }
110+ }
111+ }
0 commit comments