redux react-thunk实现异步方法

redux默认不支持异步,需要插件才能实现
	cnpm install --save redux-thunk

1、在store.js中,引入中间件,并使用

	import {createStore,applyMiddleware} from 'redux';
	import thunk from 'redux-thunk'
	const store=createStore(counter,applyMiddleware(thunk))

2、在action-type中定义action常量名,并设置与异步状态相关的同步action

	export const xx=(参数)=>{return dispatch=>{
	    异步操作,获取数据后调用同步方法改变状态
	    dispatch(同步action方法(参数))
	}}

3、引入对应的变量名等操作...

代码示例;
actions.js

/*
创建action的函数
*/

import { INCREMENT,DECREMENT } from "./action-type";

//同步action返回对象
export const increment=(num)=>({type:INCREMENT,data:num})
export const decrement=(num)=>({type:DECREMENT,data:num})
//异步action返回函数
export const incrementAsync=(num)=>{return dispatch=>{
    setTimeout(()=>{
        dispatch(increment(num))
    },1000)
}}

store.js:

import {createStore,applyMiddleware} from 'redux';
import  {counter} from './reducers'
import thunk from 'redux-thunk'

const store=createStore(counter,applyMiddleware(thunk))//第二个参数指定应用上的中间件
console.log(store);

export default store;

App.js:

import React from 'react';
import './App.css';
import {increment,decrement,incrementAsync}  from './redux/actions'
import PropTypes from 'prop-types'
import {connect} from 'react-redux'

class App extends React.Component{

  static propTypes={
    count:PropTypes.number.isRequired,
    increment:PropTypes.func.isRequired,
    decrement:PropTypes.func.isRequired,
    incrementAsync:PropTypes.func.isRequired
  }


  increment = () => {
    const num = this.refs.numSelect.value*1
    // this.props.store.dispatch(actions.increment(num));
    this.props.increment(num);
  }

  decrement = () => {
    const num = this.refs.numSelect.value*1
   
    // this.props.store.dispatch(actions.decrement(num))
    this.props.decrement(num);
  }

  incrementIfOdd = () => {
    const num = this.refs.numSelect.value*1
    // const count=this.props.store.getState();
    const count=this.props.count

    if(count%2==1) {
      this.props.increment(num);
    }
  }

	//异步调用
  incrementAsync = () => {
    const num = this.refs.numSelect.value*1
    this.props.incrementAsync(num);
  }

  render () {
    // const count = this.props.store.getState()

    const count =this.props.count;

    return (
      <div>
        <p>
          click {count} times {' '}
        </p>
        <select ref="numSelect">
          <option value="1">1</option>
          <option value="2">2</option>
          <option value="3">3</option>
        </select>{' '}
        <button onClick={this.increment}>+</button>{' '}
        <button onClick={this.decrement}>-</button>{' '}
        <button onClick={this.incrementIfOdd}>increment if odd</button>{' '}
        <button onClick={this.incrementAsync}>increment async</button>
      </div>
    )

  }

}

export default connect(
  state=>({ count:state }),
  {increment,decrement,incrementAsync}
)(App); //connect()返回一个类,第一次参数为函数,第二个参数为对象,接收参数是App组件

//第一个参数根据redux中的状态,创建变量来接收,变量也会被解构放入App标签,当作组件参数传递
//第二个参数中的内容,会被...解构当作参数传递给组件App

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值