fix: use ref for scene ID in useLeave hook to prevent stale closure

This commit is contained in:
jinyaozzz 2025-10-20 15:20:05 +08:00
parent 22e1d32415
commit 64585e60c5

View File

@ -3,7 +3,7 @@
* SPDX-license-identifier: BSD-3-Clause * SPDX-license-identifier: BSD-3-Clause
*/ */
import { useEffect, useState } from 'react'; import { useEffect, useState, useRef } from 'react';
import { useSelector, useDispatch } from 'react-redux'; import { useSelector, useDispatch } from 'react-redux';
import VERTC, { MediaType } from '@volcengine/rtc'; import VERTC, { MediaType } from '@volcengine/rtc';
import { Modal } from '@arco-design/web-react'; import { Modal } from '@arco-design/web-react';
@ -253,6 +253,8 @@ export const useJoin = (): [
export const useLeave = () => { export const useLeave = () => {
const dispatch = useDispatch(); const dispatch = useDispatch();
const { id } = useScene(); const { id } = useScene();
const idRef = useRef(id);
idRef.current = id;
return async function () { return async function () {
await Promise.all([ await Promise.all([
@ -260,7 +262,7 @@ export const useLeave = () => {
RtcClient.stopScreenCapture, RtcClient.stopScreenCapture,
RtcClient.stopVideoCapture, RtcClient.stopVideoCapture,
]); ]);
await RtcClient.stopAgent(id); await RtcClient.stopAgent(idRef.current);
await RtcClient.leaveRoom(); await RtcClient.leaveRoom();
dispatch(clearHistoryMsg()); dispatch(clearHistoryMsg());
dispatch(clearCurrentMsg()); dispatch(clearCurrentMsg());