redux 中 bindActionCreators 的实现

发布于 2022-09-20 22:51:44 字数 4084 浏览 114 评论 0

import React, { Component } from 'react';
import { createStore } from '../redux'
const INCREMENT = 'INCREMENT'
const DECREMENT = 'DECREMENT'

let initState = { num: 0 }
function reducer(state = initState, action) {
  switch (action.type) {
    case INCREMENT:
      return { num: state.num + 1 }
    case DECREMENT:
      return { num: state.num - 1 }
    default:
      return state
  }
}

let store = createStore(reducer)

class Counter extends Component {
  constructor(props) {
    super(props);
    this.state = {
      num: store.getState().num
    }
  }
  componentDidMount() {
    this.unsubscribe = store.subscribe(() => {
      this.setState({ num: store.getState().num })
    })
  }

  componentWillUnmount() {
    this.unsubscribe()
  }

  handleClick = () => {
    store.dispatch({ type: INCREMENT })
  }

  render() {
    return (
      <div>
        <p>{this.state.num}</p>
        <button onClick={this.handleClick}>
          +
        </button>
      </div>
    );
  }
}

export default Counter;

派发动作

import React, { Component } from 'react';
import { createStore } from '../redux'
const INCREMENT = 'INCREMENT'
const DECREMENT = 'DECREMENT'

let initState = { num: 0 }
function reducer(state = initState, action) {
  switch (action.type) {
    case INCREMENT:
      return { num: state.num + 1 }
    case DECREMENT:
      return { num: state.num - 1 }
    default:
      return state
  }
}

let store = createStore(reducer)

let actions = {
  increment() {
    return store.dispatch({ type: INCREMENT })
  },
  decrement() {
    return store.dispatch({ type: DECREMENT })
  }
}

class Counter extends Component {
  constructor(props) {
    super(props);
    this.state = {
      num: store.getState().num
    }
  }
  componentDidMount() {
    this.unsubscribe = store.subscribe(() => {
      this.setState({ num: store.getState().num })
    })
  }

  componentWillUnmount() {
    this.unsubscribe()
  }

  handleClick = () => {
    actions.increment()
  }

  render() {
    return (
      <div>
        <p>{this.state.num}</p>
        <button onClick={this.handleClick}>
          +
        </button>
      </div>
    );
  }
}

export default Counter;

绑定 action 的创建者

import React, { Component } from 'react';
import { createStore } from '../redux'
// import { bindActionCreators } from 'redux';
const INCREMENT = 'INCREMENT'
const DECREMENT = 'DECREMENT'

let initState = { num: 0 }
function reducer(state = initState, action) {
  switch (action.type) {
    case INCREMENT:
      return { num: state.num + 1 }
    case DECREMENT:
      return { num: state.num - 1 }
    default:
      return state
  }
}

function bindActionCreators(actionCreators, dispatch) {
  function bindActionCreator(actionCreators, dispatch) {
    return function () {
      return dispatch(actionCreators.apply(this, arguments))
    }
  }
  if (typeof actionCreators === 'function') {
    bindActionCreator(actionCreators, dispatch)
  }
  const boundActionCreator = {}
  for (const key in actionCreators) {
    boundActionCreator[key] = bindActionCreator(actionCreators[key], dispatch)
  }
  return boundActionCreator
}

let store = createStore(reducer)

let actions = {
  increment() {
    return { type: INCREMENT }
  },
  decrement() {
    return { type: DECREMENT }
  }
}
let boundActions = bindActionCreators(actions, store.dispatch)

class Counter extends Component {
  constructor(props) {
    super(props);
    this.state = {
      num: store.getState().num
    }
  }
  componentDidMount() {
    this.unsubscribe = store.subscribe(() => {
      this.setState({ num: store.getState().num })
    })
  }

  componentWillUnmount() {
    this.unsubscribe()
  }

  handleClick = () => {
    boundActions.increment()
  }

  render() {
    return (
      <div>
        <p>{this.state.num}</p>
        <button onClick={this.handleClick}>
          +
        </button>
      </div>
    );
  }
}

export default Counter;

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据

关于作者

文章
评论
28 人气
更多

推荐作者

櫻之舞

文章 0 评论 0

弥枳

文章 0 评论 0

m2429

文章 0 评论 0

野却迷人

文章 0 评论 0

我怀念的。

文章 0 评论 0

    我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
    原文