React-Redux在React中的应用

前面一篇文章我们分析了下Redux, 现在分析下React-Redux, React Redux 事实上是两个独立的产品,
应用可以使用 React 而不使用Redux ,也可以使用 Redux 而不使用 React ,但是,如果两者结合使用,没有理由不使用一个名叫 react-redux 的库这个库能够大大简化代码的书写, 我们先看一官网redux的经典案例, 从而进一步了解react-redux。

Redux的经典案例

经典案例加减

  • 定义reducer函数根据action的类型改变state
  • actions 定义指令
  • 通过createStore创建store
  • 调用store.dispatch()发出修改state的命令
import { createStore } from 'redux'

const reducer = (state = {count: 0}, action) => {
  switch (action.type){
    case 'INCREASE': return {count: state.count + 1};
    case 'DECREASE': return {count: state.count - 1};
    default: return state;
  }
}

const actions = {
  increase: () => ({type: 'INCREASE'}),
  decrease: () => ({type: 'DECREASE'})
}

const store = createStore(reducer);

// 监听每一个dispatch后返回当前最新的state
store.subscribe(() =>
  console.log(store.getState())
);

store.dispatch(actions.increase()) // {count: 1}
store.dispatch(actions.increase()) // {count: 2}
store.dispatch(actions.increase()) // {count: 3}

现在可以运用到React Component中了, 来看看吧

class Add extends Component {
  render() {
    return <div onClick={ () => store.dispatch(actions.addTodo()) }>Add</div>
  }
}

React-Redux

Official React bindings for Redux.Performant and flexible
(Redux 官方提供的 React 绑定库。 具有高效且灵活的特性。)

react-redux 的两个最主要功能:

connect :连接数据处理组件和内部UI组件;
Provider :提供包含 store的context

在这里插入图片描述
先来看看使用react-redux后的效果吧!

import { connect } from 'react-redux'

// 装饰器写法, 不懂的可以去脑补一下
@connect(
  state  => state,
  action
)
class Add extends Component {
  render() {
    return <div onClick={ () => this.props.addTodo()) }>Add</div>
  }
}

// 与装饰器一个意思的写法
export default connect(
  state => state,
  action
)

Provider源码解析

Provider 能拿到关键的store并传递给每个子组件

class Provider extends Component {
  getChildContext() {
    return { [storeKey]: this[storeKey], [subscriptionKey]: null }
  }

  constructor(props, context) {
    super(props, context)
    this[storeKey] = props.store;
  }

  render() {
    return Children.only(this.props.children)
  }
}

即通过context api将store传到子组件里面去。

connect源理简化解析

connect() 接收四个参数, 它们分别是 mapStateToProps, mapDispatchToProps, mergeProps和options.

mapStateToProps是一个函数,接受state并且返回一个对象,对象包括要传递给组件的属性,这里我们可以对state做筛选与过滤。因为我们只需要全局数据的部分数据传递给组件。

// 例子来源Todo
function mapStateToProps(state) {
  return {
    visibleTodos: selectTodos(state.todos, state.visibilityFilter),
    visibilityFilter: state.visibilityFilter
  }
}

mapDispatchToProps同样也是一个函数,返回要传递给组件的函数对象。

// actions.js
export function addTodo(text) {
  return { type: ADD_TODO, text }
}

export function completeTodo(index) {
  return { type: COMPLETE_TODO, index }
}

export function setVisibilityFilter(filter) {
  return { type: SET_VISIBILITY_FILTER, filter }
}

export default {
  addTodo, completeTodo, setVisibilityFilter,
}

// App.js
function mapDispatchToProps(dispatch) {
  return { actions: bindActionCreators(actions, dispatch) }
}

mergeProps和options目前用的少, 暂不叙述

export default function(mapStateToProps,mapDispatchToProps){
  return function(WrapedComponent){
    class ProxyComponent extends Component{
      static contextTypes = {
        store:propTypes.object
      }
      constructor(props,context){
        super(props,context);
        this.store = context.store;
        this.state = mapStateToProps(this.store.getState());
      }
      componentWillMount(){
        this.unsubscribe = this.store.subscribe(()=>{
          this.setState(mapStateToProps(this.store.getState()));
        });
      }
      componentWillUnmount(){
        this.unsubscribe();
      }
      render(){
        let actions= {};
        if(typeof mapDispatchToProps == 'function'){
          actions = mapDispatchToProps(this.store.disaptch);
        }else if(typeof mapDispatchToProps == 'object'){
          actions = bindActionCreators(mapDispatchToProps,this.store.dispatch);
        }
        return <WrapedComponent {...this.state} {...actions}/>
     }
  }
  return ProxyComponent;
 }
}

1.state的返回
connect中对于Provided父组件上传来的store,通过将状态返回

mapStateToProps(this.store.getState());

2.通过 Redux 的辅助函数 bindActionCreators(),用dispatch监听每一个action。

 bindActionCreators(mapDispatchToProps,this.store.dispatch);
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
React-Redux是一个结合了React和Redux的库,它使得在React应用集成状态管理变得更加容易。Redux是一个单一来源的状态管理库,而React-Redux提供了一组工具来帮助你将Redux的状态管理与React组件结合起来。 安装React-Redux的步骤通常是这样的: 1. 首先,确保你已经安装了Node.js和npm(Node包管理器)。 2. 在你的项目目录,打开终端或命令提示符。 3. 使用npm或yarn来安装`react-redux`和`redux`库。如果你还没有安装`redux`,可以先安装: ```bash npm install redux ``` 或者使用yarn: ```bash yarn add redux ``` 4. 安装`react-redux`: ```bash npm install react-redux ``` 或者: ```bash yarn add react-redux ``` 5. 引入到你的项目。通常会创建一个store来保存全局状态,以及将store连接到React组件上: ```javascript // 在index.js或App.js import { createStore } from 'redux'; import rootReducer from './reducers'; // 根 reducer,合并所有模块的reducer const store = createStore(rootReducer); // 如果你在使用react-redux,还需要引入Provider组件: import { Provider } from 'react-redux'; import App from './App'; // 你的React应用组件 function Root() { return ( <Provider store={store}> <App /> </Provider> ); } ReactDOM.render(<Root />, document.getElementById('root')); ``` 6. 在React组件,使用`connect`函数从store提取数据并处理dispatch动作: ```javascript import { useSelector, useDispatch } from 'react-redux'; function MyComponent() { const myState = useSelector(state => state.mySlice); // 选择store的状态 const dispatch = useDispatch(); // 获取dispatch方法 // 在组件内部使用state和dispatch // ... } ```

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值