如何在redux状态下将函数设置为值

问题描述 投票:0回答:1

我正在尝试将功能分配给redux状态

action.js

export const playerIsReady = (playPrevOrRestart) => ({
  type: PlayerType.PLAYER_IS_READY,
  payload: playPrevOrRestart,
});

reducer.js

case PlayerType.PLAYER_IS_READY:
  console.log('PLAYER_IS_READY');
  console.log(payload);
  return {
    ...state,
    isReady: true,
    playPrevOrRestart: payload,
  };
function playPrevOrRestart() {
  const sliderStart = startTime;
  const startTime1 = !_.isEmpty(sliderStart) && sliderStart >= 0 ? sliderStart : 0;
  let currentTime = player.getCurrentTime();

  if (currentTime >= startTime1 + 5) {
    player.seekTo(startTime1, true);
    return true;
  }
  return false;
}

onPlayerIsReady: (playPrevOrRestart) => dispatch(playerIsReady(playPrevOrRestart))

enter image description here

但是在redux devtool上,我也无法获取该值,当我尝试访问它时也是如此

playerState.player.playPrevOrRestart不是函数。我收到错误消息,因为该值尚未分配给该值

不可能将功能分配为状态值吗?

javascript reactjs redux redux-saga
1个回答
0
投票

当然,您可以在您的状态下存储回调。

现在,您正在调用函数,而不是将其作为参数传递。

我建议您制作一个返回它的函数:

  onPlayerIsReady: (playPrevOrRestart) => dispatch(
    () => playerIsReady(playPrevOrRestart)
  )
© www.soinside.com 2019 - 2024. All rights reserved.