redux可预测的 JS 状态容器

联合创作 · 2023-09-23 16:26

Redux 是 JavaScript 应用程序的可预测状态容器。(不要与WordPress框架——Redux框架混淆。)

它可以帮助您编写行为一致、在不同环境(客户机、服务器和本机)中运行且易于测试的应用程序。除此之外,它还提供了很好的开发体验,例如实时代码编辑和时间旅行调试器的结合。

您可以将 Redux 与 React 一起使用,或与任何其他视图库一起使用。

Actions:

// Still using constants...
import { INCREMENT_COUNTER, DECREMENT_COUNTER } from '../constants/ActionTypes';
// But action creators are pure functions returning actions
export function increment() {
  return {
    type: INCREMENT_COUNTER
  };
}
export function decrement() {
  return {
    type: DECREMENT_COUNTER
  };
}
// Can also be async if you return a function
export function incrementAsync() {
  return dispatch => {
    setTimeout(() => {
      // Yay! Can invoke sync or async actions with `dispatch`
      dispatch(increment());
    }, 1000);
  };
}
// Could also read state of a store in the callback form
export function incrementIfOdd() {
  return (dispatch, getState) => {
    const { counter } = getState();
    if (counter % 2 === 0) {
      return;
    }
    dispatch(increment());
  };
}

浏览 4
点赞
评论
收藏
分享

手机扫一扫分享

编辑
举报
评论
图片
表情
推荐
点赞
评论
收藏
分享

手机扫一扫分享

编辑
举报