VideoSDK API不使用更新后的状态。

huangapple go评论67阅读模式
英文:

VideoSDK api don't use updated state

问题

我正在使用React + ASP.NET应用程序,并使用VideoSDK API进行视频会议。

const meetingAPI = useMeeting({
  onMeetingJoined: () => {
    meetinAPI.muteMic();
    meetinAPI.disableWebcam();
    if (isCreator) {
      let intervalId = setInterval(async () => {
        console.log(
          "minutes",
          minutes +
            gameHash.ActivePlayers.length
        );
        if (
          minutes + gameHash.ActivePlayers.length >= asset.VideoTime - 10 * ActivePlayers.length
        ) {
          setVideoMinutesRunoutModalOpen(true);
        }
      }, 1000 * 60);
      setTimerId(intervalId);
    }
    setIsMeetingJoined(true);
  },
})

isCreator和gameHash是组件状态。当它们发生变化时,我期望onMeetingJoined中的值也会改变。但它始终保持初始值。我该如何解决这个问题?

我尝试过使用useCallback或useMemo,但它们没有起作用。

英文:

I'm using React + ASP.NET application and using VideoSDK API for video-meeting.

const meetingAPI = useMeeting({
onMeetingJoined: () => {
      meetinAPI.muteMic();
      meetinAPI.disableWebcam();
      if (isCreator) {
        let intervalId = setInterval(async () => {
          console.log(
            "minutes",
            minutes +
              gameHash.ActivePlayers.length
          );
          if (
            minutes + gameHash.ActivePlayers.length >= asset.VideoTime - 10 * ActivePlayers.length
          ) {
            setVideoMinutesRunoutModalOpen(true);
          }
        }, 1000 * 60);
        setTimerId(intervalId);
      }
      setIsMeetingJoined(true);
    },
})

isCreator and gameHash is component state.
When it changed, I expect value in onMeetingJoined change too.
But it's always the initial value.
How can I solve this?

I've tried to use useCallback or useMemo.
But it's not working.

答案1

得分: 0

我以前也遇到过相同的问题。
他们的支持团队非常乐于帮助。
你可以使用state ref来解决这个问题。

英文:

I've experienced same issue before.
And their support team was so helpful.
You can use state ref for that.

const stateRef = useRef();
stateRef.current = {
  minutes: minutes,
  gameHash: gameHash
};

onMeetingJoined: () => {
      meetinAPI.muteMic();
      meetinAPI.disableWebcam();
      if (stateRef.current.isCreator) {
        let intervalId = setInterval(async () => {
          console.log(
            "minutes",
            stateRef.current.minutes +
              stateRef.current.gameHash.ActivePlayers.length
          );
          if (
            stateRef.current.minutes +
              stateRef.current.gameHash.ActivePlayers.length >=
            stateRef.current.asset.VideoTime -
              10 * stateRef.current.gameHash.ActivePlayers.length
          ) {
            setVideoMinutesRunoutModalOpen(true);
          }
        }, 1000 * 60);
        setTimerId(intervalId);
      }
      setIsMeetingJoined(true);
    },

As you can see, stateRef is reference for your state.
onMeetingJoined is a third party callback so your state change is not reflected.
And this is the solution.

huangapple
  • 本文由 发表于 2023年5月10日 14:08:01
  • 转载请务必保留本文链接:https://go.coder-hub.com/76215330.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定