typescript 封装Redux store代码实现

import * as React from 'react';
import { connect as reduxConnect } from 'react-redux';
import {
  createStore,
  combineReducers,
  bindActionCreators,
  applyMiddleware
} from 'redux';
import { connectRouter, routerMiddleware } from 'connected-react-router';
import { createBrowserHistory } from 'history';
import ThunkMiddleware from 'redux-thunk';
import hoistNonReactStatic from 'hoist-non-react-statics';

import {
  reducer as homeModal,
  InitialState as homeModalInitialState,
  actions as homeModalactions
} from '../src/modal/home';

export const history = createBrowserHistory()

const createRootReducer = (history: any) =>
  combineReducers({
    homeModal,
    router: connectRouter(history)
  })

export interface IState {
  homeModal: homeModalInitialState
} 

// Strong type connect
type IMapStateToProps = (state?: IState, props?: any) => object;

export const actions = {
  homeModal: homeModalactions
}

export type IActions = typeof actions;

function createActions(dispatch: any) {
  return {
    homeModal: bindActionCreators(homeModalactions, dispatch)
  }
}

interface HOC {
  <T>(arg: T): T
}

export function connect<MapState extends IMapStateToProps>(
  mapStateToProps?: MapState
): <Props = {}>(
  reactComponent: React.FC<ReturnType<MapState> & Props & IActions>
) => React.FunctionComponent<Props & any> {
  const hoc: HOC = (Comp: any) => {
    const ConnectComp = reduxConnect(mapStateToProps, (dispatch: any) =>
      createActions(dispatch)
    )((compProps: any) => {
      const { forwardRef, ...otherProps } = compProps
      return <Comp {...otherProps} ref={forwardRef} />
    })
    const NewComp: typeof Comp = React.forwardRef((props, ref) => (
      <ConnectComp {...props} forwardRef={ref} />
    ))
    hoistNonReactStatic(NewComp, Comp)
    return NewComp
  }
  return hoc
}

export function connectClass<T, MapState extends IMapStateToProps>(
  mapStateToProps?: MapState
): (reactClass: React.ComponentClass<any, any>) => any {
  const hoc: HOC = (Comp: any) => {
    const ConnectComp = reduxConnect(mapStateToProps, (dispatch: any) =>
      createActions(dispatch)
    )((compProps: any) => {
      const { forwardRef, ...otherProps } = compProps
      return <Comp {...otherProps} ref={forwardRef} />
    })
    const NewComp: typeof Comp = React.forwardRef((props, ref) => (
      <ConnectComp {...props} forwardRef={ref} />
    ))
    hoistNonReactStatic(NewComp, Comp)
    return NewComp
  }
  return hoc
}

// 注册 fetch middleware
const registerFetchMiddleware = applyMiddleware(
  routerMiddleware(history),
  ThunkMiddleware,
) 


export const store = createStore(createRootReducer(history), registerFetchMiddleware)

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值