英文:
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.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论