【React】知识点归纳:react-redux

26 篇文章 1 订阅

一、react-redux理解

  1. 一个 react 插件库
  2. 专门用来简化 react 应用中使用 redux

React-Redux 将所有组件分成两大类

  1. UI 组件
    a. 只负责 UI 的呈现,不带有任何业务逻辑
    b. 通过 props 接收数据(一般数据和函数)
    c. 不使用任何 Redux 的 API
    d. 一般保存在 components 文件夹下
  2. 容器组件
    a. 负责管理数据和业务逻辑,不负责 UI 的呈现
    b. 使用 Redux 的 API
    c. 一般保存在 containers 文件夹下

相关 API

  1. Provider
    让所有组件都可以得到 state 数据
<Provider store={store}>
<App />
</Provider>
  1. connect()
    用于包装 UI 组件生成容器组件
import { connect } from 'react-redux'
connect(
	mapStateToprops,
	mapDispatchToProps
)(Counter)
  1. mapStateToprops()
    将外部的数据(即 state 对象)转换为 UI 组件的标签属性
const mapStateToprops = function (state) {
	return {
		value: state
	}
}
  1. mapDispatchToProps()
    将分发 action 的函数转换为 UI 组件的标签属性
    简洁语法可以直接指定为 actions 对象或包含多个 action 方法的对象

二、使用 react-redux

对比 使用redux方法写此案例 网址:https://blog.csdn.net/qq_41846861/article/details/86701986

  1. 下载依赖包
npm install --save react-redux
  1. redux/action-types.js
    不变
    参考网址:https://blog.csdn.net/qq_41846861/article/details/86701986
  2. redux/actions.js
    不变
    参考网址:https://blog.csdn.net/qq_41846861/article/details/86701986
  3. redux/reducers.js
    不变
    参考网址:https://blog.csdn.net/qq_41846861/article/details/86701986
  4. components/counter.jsx
import React,{Component} from 'react'
// import {INCREMENT,DECREMENT} from '../redux/action-types'
import PropTypes from 'prop-types'
// import * as actions from '../redux/actions'

export default class Counter extends Component{

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

    increment = () => {
        //1.得到选择的增加数量
        const number = this.select.value * 1
        //2.调用store的方法更新状态
        // this.props.store.dispatch({type:INCREMENT,data:number})
        this.props.increment(number)
    }

    decrement = () => {
        const  number = this.select.value*1
        //2.调用store的方法更新状态
        // this.props.store.dispatch({type:DECREMENT,data:number})
        this.props.decrement(number)
    }

    incrementIfOdd = () =>{
        //1.得到选择的更新数量
        const number = this.select.value*1
        //2.得到原本的count状态
        const count  = this.props.count
        //判断是否为偶数 满足则更新
        if (count%2 === 1){
            //2.调用store的方法更新状态
            // this.props.store.dispatch({type:INCREMENT,data:number})
            this.props.increment(number)
        }
    }

    incrementAsync  = () =>{
        //1.得到选择的更新数量
        const number = this.select.value*1
        //启动延迟定时器
        setTimeout(() =>{
            //3.更新状态
            // this.props.store.dispatch({type:INCREMENT,data:number})
            this.props.increment(number)
        },1000)
    }

    render (){
        const {count} = this.props
        // debugger
        return(
            <div>
                <p>Click{count}times</p>
                <div>
                    <select ref={select => this.select = select}>
                        <option value='1'>1</option>
                        <option value='2'>2</option>
                        <option value='3'>3</option>
                    </select>&nbsp;
                    <button onClick={this.increment}>+</button>&nbsp;
                    <button onClick={this.decrement}>-</button>&nbsp;
                    <button onClick={this.incrementIfOdd}>increment if odd</button>&nbsp;
                    <button onClick={this.incrementAsync}>increment async</button>&nbsp;
                </div>
            </div>
        )
    }
}
  1. containters/app.jsx
import React from 'react'
import {connect} from 'react-redux'

import {increment,decrement} from '../redux/actions'
import Counter from '../components/counter'
export default connect(
    state => ({count:state}),
    {increment,decrement}
)(Counter)
  1. index.js
import React from 'react';
import ReactDOM from 'react-dom';
import {Provider} from 'react-redux'

import App from './containers/app';
import store from './redux/store'

// ReactDOM.render(<App store={store}/>, document.getElementById('root'));

ReactDOM.render((
    <Provider store={store}>
         <App/>
    </Provider>
), document.getElementById('root'));

注意:

  1. index.js中不再有 注册(订阅)监听
// 注册(订阅)监听, 一旦状态发生改变, 自动重新渲染
store.subscribe(render)
  1. 通过connect()来用包装 UI 组件生成容器组件。
    想要详细了解React-redux中connect方法,请参考网址:React-redux中connect方法

三、存在问题

  1. redux 默认是不能进行异步处理的
  2. 应用中又需要在 redux 中执行异步任务(ajax, 定时器)
    若想详细了解,可参考网址:redux 异步编程
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值