美文网首页技术开源共享
React常用中间件及组件

React常用中间件及组件

作者: 小小的开发人员 | 来源:发表于2019-06-02 01:25 被阅读0次

中间件

redux默认只处理同步,对于API请求这样的异步任务则无能为力。

// constants/ActionTypes.js
export const ADD = 'ADD'
export const SQUARE = 'SQUARE'
export const SET = 'SET'

// action/math.js
import { ADD, SQUARE, SET } from '../constants/ActionTypes'
export const add = num => ({ type: ADD, num })
export const square = { type: SQUARE }
export const setNum = num => ({type: SET,num})

// reduce/math.js
import { ADD, SQUARE,SET } from '../constants/ActionTypes'
const math = (state = 10, action) => {
  switch (action.type) {
    case ADD:
      return state + action.num
    case SQUARE:
      return state * state
    case SET:
      return action.num
    default:
      return state
  }
}
export default math

// index.js
import store from './store'
import React from 'react'
import ReactDOM from 'react-dom'
import { add, square, setNum } from './action/math'
import axios from 'axios'
let uri = 'https://jsonplaceholder.typicode.com/posts/2'
const render = () => ReactDOM.render(
  <div store={store}>
    <p>{store.getState().math}</p>
    <input type="button" onClick={() => {axios.get(uri).then(res => {store.dispatch(store.dispatch(setNum(res.data.id)))})}} value="设置Num" />
    <input type="button" onClick={() => store.dispatch(add(1))} value="+1" />
    <input type="button" onClick={() => store.dispatch(add(2))} value="+2" />
    <input type="button" onClick={() => store.dispatch(square)} value="乘方" />
  </div>,
  document.getElementById('root')
)
render()
store.subscribe(render)

  虽然API是异步操作,但store.dispatch并不是异步,而axios通过get方法请求回来数据后,store.dispatchaxios中的then方法中同步取得数据。

【redux-thunk】
  如果把异步的请求和非常复杂的逻辑都放在组件里实现,组件会变得臃肿,应利用redux-thunk。将这些复杂的逻辑放到action中去处理。redux-thunk检测到action是一个函数会进行处理。

首先,使用npm进行安装

npm install --save redux-thunk

然后,使用applyMiddleware来使用thunk中间件。

import { createStore, applyMiddleware } from 'redux'
import thunk from 'redux-thunk'
import rootReducer from '../reducer'
export default createStore(rootReducer,applyMiddleware(thunk))

修改的代码如下:

// action/math.js
import { ADD, SQUARE, SET } from '../action/ActionTypes'
import axios from 'axios'
const uri = 'https://jsonplaceholder.typicode.com/posts/2'
export const add = num => ({ type: ADD, num })
export const square = { type: SQUARE }
export const setNum = () => (dispatch, getState) => {
  return axios.get(uri).then(res => {
    dispatch({
      type: SET,
      num: res.data.id
    })
  })
}

// index.js
import store from './store'
import React from 'react'
import ReactDOM from 'react-dom'
import { add, square, setNum } from './action/math'
const render = () => ReactDOM.render(
  <div store={store}>
    <p>{store.getState().math}</p>
    <input type="button" onClick={() => store.dispatch(setNum())} value="设置Num" />
    <input type="button" onClick={() => store.dispatch(add(1))} value="+1" />
    <input type="button" onClick={() => store.dispatch(add(2))} value="+2" />
    <input type="button" onClick={() => store.dispatch(square)} value="乘方" />
  </div>,
  document.getElementById('root')
)
render()
store.subscribe(render)

【注意】 redux的中间件,而不是react的。


从上面的图,我们可以看出,中间件在dispatch这一步起作用,仍然返回一个action对象,最后交给reducer处理。

【redux-saga】
  超大型项目的异步处理,用redux-saga


【redux-persist】
  在开发的过程中,数据用redux管理,觉得希望将数据持久化保存,也就是说当用户下一次打开app或网站的时候,我们希望浏览器/APP自动加载出上次的数据。

【redux-immutable】
  统一数据格式

常用组件

【react-loadable】
  异步组件react-loadable


【withRouter】
只有点击路由时才会加载相应的js



【react-transition-group】
react-transition-group实现多个DOM元素的动画


相关文章

  • React常用中间件及组件

    中间件 redux默认只处理同步,对于API请求这样的异步任务则无能为力。   虽然API是异步操作,但store...

  • react-native 初体验

    react-native 常用组件 基础组件: 交互组件(跨平台) TabBarIos 常用属性: TabBarI...

  • React Hooks 使用指南

    Hook 是 React 16.8 开始支持的新特性,旨在用函数式组件代替类式组件。本文记录常用Hook用法及注意...

  • react组件通信

    react组件通信是一个常用功能,在做react项目中经常遇到 React组件层级关系 在了解Reat组件通讯之前...

  • React Native常用组件之ScrollView

    配置React Native的开发环境 React Native常用组件之ScrollView React Nat...

  • react-native 新建项目

    指定版本新建项目 第三库组件 React Native常用组件 @react-navigation 路由导航@re...

  • 关于混编

    常用reactnative 组件 react-native-swiper 图片轮播器 react-native-m...

  • React Native开发中常用三方组件库大全

    React Native开发中常用三方组件大全 作者整理的一套常用的React Native开发中使用到的三方组件...

  • React开发相关资料

    React-Router 中文简明教程 总结 React 组件的三种写法 及最佳实践 [涨经验] React-UI组件

  • 一起来点React  Native

    一起来点React Native 配置React Native的开发环境 React Native常用组件之Vie...

网友评论

    本文标题:React常用中间件及组件

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