react-thunk用法记录

作者: chouchou723 | 来源:发表于2018-11-15 14:40 被阅读0次

首先在父级进行注入:

import rootReducer from 'reducers';
import { createStore, applyMiddleware } from 'redux';
import thunk from 'redux-thunk';
const store = createStore(rootReducer,applyMiddleware(thunk))//中间件注入
 <Provider store={store}>
  {routes}
  </Provider>

多个reducer合并到一个:

// reducer/index.js
import { combineReducers } from 'redux'
import * as todoList from './todoList'

const rootReducer = combineReducers({
  ...todoList,
})

export default rootReducer

reducer文件

//todoList.js
import { handleActions } from 'redux-actions'
//使用了handleActions 方便书写action
const todo = (state, action) => {//中间方法
  switch (action.type) {
    case 'ADD_TODO':
      return {
        id: action.id,
        text: action.text,
        completed: false // 刚传入的待办项未完成
      }
    case 'TOGGLE_TODO':
      if (state.id !== action.id) {
        return state
      }
      return Object.assign({}, state, {
        completed: !state.completed
      })
    default:
      return state
  }
}
const todoListInit = [{//初始化值
    id: -3,
    text: 'coding',
    completed: false,
  }, {
    id: -2,
    text: '打篮球',
    completed: false,
  }, {
    id: -1,
    text: 'reading',
    completed: true,
  }]
export const todoList = handleActions({
  'ADD_TODO'(state, action) {
    return [
      ...state,
      todo(undefined, action.payload)
    ]
  },
  'TOGGLE_TODO'(state, action) {
    console.log(action)
    return state.map(t => todo(t, action.payload))
  },
  'DEL_TODO'(state, action) {
    return state.filter(t => t.id !== action.payload.id)
  },
  'AAA_TODO'(state, action) {
    console.log(action.val)
    return state
  },
  'CHANGE_TODO'(state,action) {
    console.log(action)
    return state
  }
}, todoListInit)

action文件:

// action/todoList.js
import { createAction } from 'redux-actions'

export const addTodo = createAction('ADD_TODO') //标准的redux创建action
export const setVisibility = createAction('SET_VISIBILITY')
export const toggleTodo = createAction('TOGGLE_TODO')
export const delTodo = createAction('DEL_TODO')
export const changeTodo = createAction('CHANGE_TODO')
export const initFetch = (val) => dispatch => {
  dispatch({type: 'AAA_TODO',val:val })
}

组件当中的使用:

import { addTodo,toggleTodo,delTodo,initFetch,changeTodo } from 'actions/todoList' //引入对应方法
  submit = (e) => {  //redux的直接使用
        e.preventDefault()
        console.log(this)
        if(!this.input.value.trim()){
            return
        }
        this.props.dispatch(addTodo({//标准的redux调用,参数会作为action.payload
            id: nextTodoId++,
            text: this.input.value,
            type: 'ADD_TODO',
        }))
        this.input.value = ''
    }
componentDidMount() {
        console.log(this.state.a)
        // this.props.dispatch({type:'AAA_TODO'})
        this.props.dispatch(initFetch({a:123}))//必须使用中间件thunk才能如此调用;
    }

相关文章

  • react-thunk用法记录

    首先在父级进行注入: 多个reducer合并到一个: reducer文件 action文件: 组件当中的使用:

  • react --- redux使用

    react-reduxReact插件,作用:方便在React项目中使用Redux react-thunk中间件,作...

  • 2018-11-18NSOperation

    记录NSOperation用法.

  • Laravel 开发笔记

    Laravel 笔记 前言 记录 Laravel 开发中的问题,及笔记。 用法 Validator 类的用法 用法...

  • react-thunk 项目实践

    简介: redux 可以通过 dispath 一个action 后, 更新state,这叫做同步; 但是actio...

  • git 用法记录

    Git 开发部署流程 https://blog.csdn.net/cs958903980/article/deta...

  • vuex用法记录

    Vuex简介 Vuex 是一个专为 Vue.js 应用程序开发的状态管理模式 State: 驱动应用的数据源; V...

  • ActiveRecord 用法记录

    主要用来记录自己遇到的问题 update如何使用自己的值描述: 批量设置记录的某一个字段值加一

  • Markdown用法记录

    标题 在 Markdown 中,如果一段文字被定义为标题,只要在这段文字前加 # 号即可,以此类推,总共六级标题。...

  • tushare用法记录

    以中通客车为例,tushare 用法记录下

网友评论

    本文标题:react-thunk用法记录

    本文链接:https://www.haomeiwen.com/subject/lrdyfqtx.html