在进行重组时应在哪里设置`setInterval`?

时间:2018-11-29 14:26:39

标签: javascript react-native recompose

我正在尝试构建一个简单的计时器,该计时器将在点击时启动和停止。 我所有的项目都是基于功能组件的(使用重组),所以我不确定在哪里设置setInterval

这是我尝试过的事情,直到我完全迷失了存储setInterval的位置,因此我可以在onStop fn上清除它(将在按钮上触发) -就像在功能组件中一样,没有this我可以放置计时器并将其从...中移除...功能组件的工作方式是什么?

https://codepen.io/anon/pen/jQQZrm?editors=0010

有什么建议吗? -使用本机 谢谢。

2 个答案:

答案 0 :(得分:2)

您在这里需要3个不同的状态处理程序:stopTimerstartTimerupdateValue(我使用的命名与您的代码略有不同)。

startTimer中,您需要创建一个由计时器运行updateValue的计时器。换句话说,您需要从另一个间接调用一个状态处理程序。

no way doing that。但。您可以将这些处理程序分为2组:“值+更新值”和“停止计时器+起始计时器+ intervalId”。然后,您将能够从第二个第一个集合中获取状态处理程序,例如props

const EnchanceApp = compose(
  withStateHandlers({
    timer: 0,
  }, {
    updateValue: ({timer}) => 
        () => ({timer: timer + 1})
  }),
  withStateHandlers({
    timerId: 0,
  }, {
    startTimer: ({timerId}, {updateValue}) => 
        () => {
            clearInterval(timerId);
            return {
                timerId: setInterval(updateValue, 1000)
            };
        },
    stopTimer: ({timerId}) => 
        () => clearInterval(timerId)
  })
)(App);

答案 1 :(得分:1)

工作完美,我的代码示例:

const BgList = ({ bgs }) => (
  <PoseGroup>
    {bgs.map(item => <StyledBg key={item} style={{backgroundImage: 'url(/img/'+item+'.jpg)'}} />)}
  </PoseGroup>
);

const enhance = compose(
  withStateHanlders(
    () => ({
      index: 0,
      isVisible: false,
      bgs: _.shuffle([0,1,2,3]),
      timerId: 0,
    }),
    {
      startTimer: () => ({timerId}, {updateValue}) => {
        clearInterval(timerId);
        return {
            timerId: setInterval(updateValue, 5000)
        };
      },
      stopTimer: ({timerId}) => () => clearInterval(timerId),
      updateValue: ({bgs}) => 
        () => {
          return ({bgs: _.shuffle(bgs)})
        },
    },
  ),
  lifecycle({
    componentDidMount() {
      const {timerId, updateValue} = this.props;
      this.props.startTimer({timerId}, {updateValue})
    }
  }),

)

const BlockAnimated = enhance(({
  bgs
}) => {
  return (
    <BgList bgs={bgs} />