react typesafe-actions

主要是为了,给redux,定义

文件夹---目录
index.tsx app首页
actions
	index 入口
	project.ts
reducers
	index 入口
	project.ts

index.tsx

import { createStore, applyMiddleware } from 'redux'
import { Provider } from 'react-redux'
import { createLogger } from 'redux-logger'
import thunk from 'redux-thunk'
import reducer from './reducers'
// import { getAllProducts } from './actions'

const middleware:any[] = [ thunk ];
if (process.env.NODE_ENV !== 'production') {
  middleware.push(createLogger());
}

const store = createStore(
  reducer,
  applyMiddleware(...middleware)
)

 <Provider store={store}>
        <AppProvider>
          <DevTools />
          <BrowserRouter>
           <App />
          </BrowserRouter>
        </AppProvider>
       </Provider>

reducers/index.ts

import { combineReducers } from 'redux'
import { StateType } from 'typesafe-actions';
import {project} from './project'
import {project1} from './project1'
const reducers = {project,project1};
export type RootState = StateType<typeof reducers>;

export default combineReducers({
  project,
  project1
});

reducers/project.ts

import { action, ActionType,createAction } from 'typesafe-actions';

// with "import * as ..."
import * as todos from '../actions/project';
export type TodosAction = ActionType<typeof todos>;

export interface Project {
  visible:boolean,
  todos:any[]
}
const initialProject = {
  visible:false,
  todos:[]
}
export function project(
  state:Project = initialProject,
  action:TodosAction
):Project{
  console.log(action,'19');
  switch(action.type){
    case 'VISIBLE':
      return { ...state, visible: action.payload }
    case 'ADD_TODO':
      return { ...state,todos:[...state.todos,...action.payload]}
      default:
      return initialProject
  }
}

actions/index.ts

好像暂时,没用到
import { ActionType } from 'typesafe-actions';

import * as profileActions from './project';

export type ProfileAction = ActionType<typeof profileActions>;

export type RootAction = ProjectAction;

actions/project.ts

import { createAction } from 'typesafe-actions';
import { Dispatch } from 'redux';
// https://github.com/piotrwitek/typesafe-actions/blob/HEAD/src/create-action.spec.ts
// 这个链接,教你action如何定义和如何调用

// -设置抽屉显示 ,隐藏
export const drawerVisible1 = createAction('VISIBLE')<boolean>();
// drawerVisible1(true); // => { type: 'VISIBLE', payload: true }
//2种调用形式
//dispatch(drawerVisible1(true)) 
//dispatch({ type: 'VISIBLE', payload: true })

export const withPayload = createAction('ADD_TODO')<any>();
// withPayload(10); // => { type: 'CREATE_ACTION', payload: 10 }
//2种调用形式
//dispatch(withPayload(10)) 
//dispatch({ type: 'CREATE_ACTION', payload: 10 })

//移步 thunk
export const setTodos = (val: boolean) =>
  async (dispatch: Dispatch, getState: any) => {
    const { visible } = getState().project;
    dispatch(drawerVisible1(val));
  }

//异步 调用
//dispatch(setTodos(true))

在组件使用语法

import { useSelector, useDispatch } from "react-redux";
import { RootState } from '../../reducers';
import { withPayload } from '../../actions/project';

 const visible = useSelector((state:RootState) => state.project.visible);
 dispatch(withPayload([{name:'gpc'}]))
因为公司用的这个玩意,我不得不学
这里我也不想深入研究了,我准备在demo里面使用redux-toolkit了
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值