reactjs - 即使我设置了它,我的 usestate 也没有更新
问题描述
我正在打印stream
变量,但null
在更新它之后我什至得到了。
当我的设置状态被调用时,我应该重新渲染整个组件吗?由于此代码执行一次,如果是这样,该怎么做?
const [stream, setStream] = useState(null);
async function getLocalStream() {
let isFront = true;
const devices = await mediaDevices.enumerateDevices();
if (!stream) {
let s;
try {
s = await mediaDevices.getUserMedia({
audio: true,
video: true,
});
setStream(s); //here i am setting my state
} catch (e) {
console.error(e);
}
}
}
useEffect(() => {
socket.current = io.connect('http://192.8.1.13:3001');
socket.current.emit('new-room', route.params.roomid, route.params.name);
socket.current.on('room-created', async (id, roomid, name) => {
getLocalStream(); // calling function
});
socket.current.on('join-room', async (id, roomid, name) => {
getLocalStream();
});
socket.current.on('new-user', async (id, roomid, name) => {
console.log('stream', stream); // my value is always null
});
}, []);
解决方案
推荐阅读
- javascript - 如何手动创建 http.IncomingMessage 流?
- javascript - 使用 AJAX 表单时如何让 Chrome 将用户的输入保存到他们的自动完成历史记录中
- java - 将所选日期与当前日期进行比较,不能超过 1 天
- vb.net - 从数据行 vb 从字符串转换为整数类型
- google-apps-script - 使用 GAS 将段落类型更改为列表项类型
- python - 使用套接字在 android 设备上创建点击(从 python 转换为 C)
- docker - 如何在 ECS 中运行 docker-compose?(不是从我的窗口手动)
- c# - 将图像转换为字节并将其分成块
- google-coral - Coral 开发板 - 无法运行演示
- node.js - 将 readstream 导入响应使其一次性使用