我可能会建议采用不同的方式:仅存储计算存储中经过的时间所需的状态,并让组件设置自己的时间间隔,无论它们希望更新显示的频率如何。
这将动作分派保持在最低限度——只分派启动和停止(和重置)计时器的动作。请记住,每次分派动作时都会返回一个新的状态对象,然后每个connected 组件都会重新渲染(即使它们使用优化来避免在包装的组件内进行过多的重新渲染)。此外,许多动作调度可能会使调试应用程序状态更改变得困难,因为您必须TICK与其他动作一起处理所有s。
下面是一个例子:
// Action Creators
function startTimer(baseTime = 0) {
  return {
    type: "START_TIMER",
    baseTime: baseTime,
    now: new Date().getTime()
  };
}
function stopTimer() {
  return {
    type: "STOP_TIMER",
    now: new Date().getTime()
  };
}
function resetTimer() {
  return {
    type: "RESET_TIMER",
    now: new Date().getTime()
  }
}
// Reducer / Store
const initialState = {
  startedAt: undefined,
  stoppedAt: undefined,
  baseTime: undefined
};
function reducer(state = initialState, action) {
  switch (action.type) {
    case "RESET_TIMER":
      return {
        ...state,
        baseTime: 0,
        startedAt: state.startedAt ? action.now : undefined,
        stoppedAt: state.stoppedAt ? action.now : undefined
      };
    case "START_TIMER":
      return {
        ...state,
        baseTime: action.baseTime,
        startedAt: action.now,
        stoppedAt: undefined
      };
    case "STOP_TIMER":
      return {
        ...state,
        stoppedAt: action.now
      }
    default:
      return state;
  }
}
const store = createStore(reducer);
请注意,动作创建者和化简器仅处理原始值,不使用任何类型的间隔或TICK动作类型。现在,组件可以轻松订阅此数据并根据需要随时更新:
// Helper function that takes store state
// and returns the current elapsed time
function getElapsedTime(baseTime, startedAt, stoppedAt = new Date().getTime()) {
  if (!startedAt) {
    return 0;
  } else {
    return stoppedAt - startedAt + baseTime;
  }
}
class Timer extends React.Component {
  componentDidMount() {
    this.interval = setInterval(this.forceUpdate.bind(this), this.props.updateInterval || 33);
  }
  componentWillUnmount() {
    clearInterval(this.interval);
  }
  render() {
    const { baseTime, startedAt, stoppedAt } = this.props;
    const elapsed = getElapsedTime(baseTime, startedAt, stoppedAt);
    return (
      <div>
        <div>Time: {elapsed}</div>
        <div>
          <button onClick={() => this.props.startTimer(elapsed)}>Start</button>
          <button onClick={() => this.props.stopTimer()}>Stop</button>
          <button onClick={() => this.props.resetTimer()}>Reset</button>
        </div>
      </div>
    );
  }
}
function mapStateToProps(state) {
  const { baseTime, startedAt, stoppedAt } = state;
  return { baseTime, startedAt, stoppedAt };
}
Timer = ReactRedux.connect(mapStateToProps, { startTimer, stopTimer, resetTimer })(Timer);
您甚至可以在具有不同更新频率的同一数据上显示多个计时器:
class Application extends React.Component {
  render() {
    return (
      <div>
        <Timer updateInterval={33} />
        <Timer updateInterval={1000} />
      </div>
    );
  }
}
您可以在此处查看具有此实现的有效 JSBin:https : //jsbin.com/dupeji/12/edit?js , output