前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >【taro react】---- 【使用 redux 的配置笔记】

【taro react】---- 【使用 redux 的配置笔记】

作者头像
Rattenking
发布2022-01-06 20:13:10
1.1K0
发布2022-01-06 20:13:10
举报
文章被收录于专栏:RattenkingRattenking

1. 目标

  1. 学会 yarn 或 npm 安装中间件
  2. 学会配置 redux 的 store
  3. 学会 store 的接入和使用

2. 安装中间件

安装redux用到的中间件: redux react-redux redux-thunk redux-logger

代码语言:javascript
复制
$ yarn add redux react-redux redux-thunk redux-logger
# 或者使用 npm
$ npm install --save redux react-redux redux-thunk redux-logger

3. 配置 store

在项目 src 目录下新增一个 store 目录,在目录下增加 index.js 文件用来配置 store。

  1. src/store/index.js
代码语言:javascript
复制
import { createStore, applyMiddleware, compose } from 'redux';
import thunk from 'redux-thunk';
import reducer from '../reducers';

// window.__REDUX_DEVTOOLS_EXTENSION__  可使用Redux DevTools插件
const composeEnhancers = window.__REDUX_DEVTOOLS_EXTENSION_COMPOSE__ ? window.__REDUX_DEVTOOLS_EXTENSION_COMPOSE__({}) : compose;

// 使用Redux-thunk中间件
const enhancer = composeEnhancers(applyMiddleware(thunk));

// 创建store
const store = createStore(reducer, enhancer);

export default store;

4. 接入 store

在项目入口文件 app.js 中使用 redux 中提供的 Provider 组件将前面写好的 store 接入应用。

  1. src/app.js
代码语言:javascript
复制
import React, { Component } from 'react'
import { Provider } from 'react-redux'
import store from './store'
import './app.css'

class App extends Component {
  // 在 App 类中的 render() 函数没有实际作用
  // 请勿修改此函数
  render () {
    return (
      <Provider store={store}>
        {this.props.children}
      </Provider>
    )
  }
}

export default App

5. 开始使用

  1. constants 目录,用来放置所有的 action type 常量
  2. actions 目录,用来放置所有的 actions
  3. reducers 目录,用来放置所有的 reducers
5.1 以官方示例,开发一个简单的加、减计数器功能

新增 action type

  1. src/constants/counter.js
代码语言:javascript
复制
export const ADD = 'ADD'
export const MINUS = 'MINUS'
5.2 新增 reducer 处理
  1. src/reducers/counter.js
代码语言:javascript
复制
import { ADD, MINUS } from '../constants/counter'

const INITIAL_STATE = {
  num: 0
}

export default function counter (state = INITIAL_STATE, action) {
  switch (action.type) {
    case ADD:
      return {
        ...state,
        num: state.num + 1
      }
    case MINUS:
      return {
        ...state,
        num: state.num - 1
      }
    default:
      return state
  }
}
  1. src/reducers/index.js
代码语言:javascript
复制
import { combineReducers } from 'redux'
import counter from './counter'
import { defaultIndex } from './defaultIndex'
import { commonCart } from './commonCart'
import { goodsDetail } from './goodsDetail'

export default combineReducers({
  counter,
  defaultIndex,
  commonCart,
  goodsDetail 
})
5.3 新增 action 处理
  1. src/actions/counter.js
代码语言:javascript
复制
import {
  ADD,
  MINUS
} from '../constants/counter'

export const add = () => {
  return {
    type: ADD
  }
}
export const minus = () => {
  return {
    type: MINUS
  }
}

// 异步的 action
export function asyncAdd () {
  return dispatch => {
    setTimeout(() => {
      dispatch(add())
    }, 2000)
  }
}

6. 页面或组件使用

在页面(或者组件)中进行使用,我们将通过 redux 提供的 connect 方法将 redux 与我们的页面进行连接。

  1. src/pages/index/index.js
代码语言:javascript
复制
import React, { Component } from 'react'
import { connect } from 'react-redux'
import { View, Button, Text } from '@tarojs/components'

import { add, minus, asyncAdd } from '../../actions/counter'

import './index.css'


@connect(({ counter }) => ({
  counter
}), (dispatch) => ({
  add () {
    dispatch(add())
  },
  dec () {
    dispatch(minus())
  },
  asyncAdd () {
    dispatch(asyncAdd())
  }
}))
class Index extends Component {
  render () {
    return (
      <View className='index'>
        <Button className='add_btn' onClick={this.props.add}>+</Button>
        <Button className='dec_btn' onClick={this.props.dec}>-</Button>
        <Button className='dec_btn' onClick={this.props.asyncAdd}>async</Button>
        <View><Text>{this.props.counter.num}</Text></View>
        <View><Text>Hello, World</Text></View>
      </View>
    )
  }
}

export default Index

7. 注意

  1. connect 方法接受两个参数 mapStateToProps 与 mapDispatchToProps
  2. mapStateToProps,函数类型,接受最新的 state 作为参数,用于将 state 映射到组件的 props
  3. mapDispatchToProps,函数类型,接收 dispatch() 方法并返回期望注入到展示组件的 props 中的回调方法

8. 官方文档

使用 Redux

本文基本都是按照官方文档一步一步配置,仅作为开发记录,有问题,请反馈联系!

本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
原始发表:2021-11-03 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 1. 目标
  • 2. 安装中间件
  • 3. 配置 store
  • 4. 接入 store
  • 5. 开始使用
    • 5.1 以官方示例,开发一个简单的加、减计数器功能
      • 5.2 新增 reducer 处理
        • 5.3 新增 action 处理
        • 6. 页面或组件使用
        • 7. 注意
        • 8. 官方文档
        相关产品与服务
        消息队列 TDMQ
        消息队列 TDMQ (Tencent Distributed Message Queue)是腾讯基于 Apache Pulsar 自研的一个云原生消息中间件系列,其中包含兼容Pulsar、RabbitMQ、RocketMQ 等协议的消息队列子产品,得益于其底层计算与存储分离的架构,TDMQ 具备良好的弹性伸缩以及故障恢复能力。
        领券
        问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档