我有一个useEffect()
,用于检查是否有应播放的消息声音处于触发状态的布尔值,并在播放后将活动消息触发器的状态设置为false。
但是,useEffect()陷入死循环,使应用程序崩溃。可能是因为更改状态会再次触发它。。。
通常,使用useState可以很容易地用useEffect(() => {logic}, [trigger])
在我的情况下,我没有使用useState,而是使用reducer更改状态。
Edit: The weird thing is, the reducer sometimes works to modify state, and sometimes it does not. It will execute without errors but the state remains unchanged.
让我告诉我我的评论代码:
import React, { useEffect } from "react";
import { getCachedImage } from "../helpers";
const MessageNotification = (props) => {
const messageImg= getCachedImage("/ui/newmessage.png");
// Function that plays given sound
function playSound(soundFile) {
let audio = new Audio("/audio/messages/" + soundFile);
audio.play();
}
// Check if a Message is set to active. If so, execute logic
useEffect(() => {
// Get messages from state and find the message with "active" set to true
const messagesState = props.state.messages;
const activeMessage = messagesState.find((element) => element.active === true);
if (activeMessage) {
playSound(activeMessage.audio);
// Mark the message as userNotified and set it to inactive in state
let updatedMessagesState = messagesState ;
let index = messagesState.indexOf(activeMessage);
if (~index) {
updatedMessagesState[index].userNotified= true;
updatedMessagesState[index].active = false;
}
/* This is the weird part, the updatedMessagesState is correct,
but the dispatch reducer does not pass it to state.
This does work when I remove the useEffect
(but that gives me a fat red warning in console) */
props.dispatch({ type: "UPDATE_MESSAGES", payload: updatedMessagesState });
}
});
return (
<div>
<img src={"images" + messageImg} alt="message" width="90" height="90"></img>
</div>
);
};
export default MessageNotification;
如您所见,我不使用useState而是使用reducer。据我所知,我经常发现与以下内容相关的解决方案不是我的解决方案:
// Not applicable solution for me, since I use reducer
const [trigger] = useState();
useEffect(() => {
// Logic here
}, [trigger]);
编辑:由于reducer在useEffect中使用时似乎并未修改状态,因此让我发布其代码:
const reducer = (state, action) => {
switch (action.type) {
case "UPDATE_MESSAGES":
return { ...state, messages: action.payload };
default:
throw new Error();
}
};
export default reducer;
答案 0 :(得分:2)
尝试为您的useEffect
添加依赖项,例如:
useEffect(() => {
if (activeMessage) {
playSound(activeMessage.audio);
//mark the message as userNotified and set it to inactive in state
let updatedMessagesState = messagesState ;
let index = messagesState.indexOf(activeMessage);
if (~index) {
updatedMessagesState[index].userNotified= true;
updatedMessagesState[index].active = false;
}
props.dispatch({ type: "UPDATE_MESSAGES", payload: updatedMessagesState });
}
}, [activeMessage]);
通过不指定依赖项数组,您的useEffect
将在每个渲染器上运行,从而创建无限循环。
此外,您正在尝试直接在此行上修改道具(这是一种反模式):
const messagesState = props.state.messages;
尝试将其更改为此:
const messagesState = [...props.state.messages];
此外,由于let index = messagesState.indexOf(activeMessage);
是对象数组,因此messagesState
将不起作用。要获取活动消息的索引,请尝试以下操作:
let index = messagesState.map(message => message.active).indexOf(true);
答案 1 :(得分:1)
我认为,如果您添加props.state.messages作为依赖项,该问题将得到解决。另外,如果仅在useEffect中使用messagesState和messageState,则应将此变量移至该块:
useEffect(() => {
const messagesState = props.state.messages;
const messagesState = messagesState.find((element) => element.active === true);
if (activeMessage) {
playSound(activeMessage.audio);
//mark the message as userNotified and set it to inactive in state
let updatedMessagesState = messagesState ;
let index = messagesState.indexOf(activeMessage);
if (~index) {
updatedMessagesState[index].userNotified= true;
updatedMessagesState[index].active = false;
}
/* This is the weird part, the updatedMessagesState is correct,
but the dispatch reducer does not pass it to state.
This does work when I remove the useEffect
(but that gives me a fat red warning in console) */
props.dispatch({ type: "UPDATE_MESSAGES", payload: updatedMessagesState });
}
}, [props.state.messages]);
答案 2 :(得分:1)
// Check if a Message is set to active. If so, execute logic
useEffect(() => {
// Get messages from state and find the message with "active" set to true
const messagesState = props.state.messages;
const activeMessage = messagesState.find((element) => element.active === true);
if (activeMessage) {
playSound(activeMessage.audio);
// Mark the message as userNotified and set it to inactive in state
let updatedMessagesState = messagesState ;
let index = messagesState.indexOf(activeMessage);
if (~index) {
updatedMessagesState[index].userNotified= true;
updatedMessagesState[index].active = false;
}
/* This is the weird part, the updatedMessagesState is correct,
but the dispatch reducer does not pass it to state.
This does work when I remove the useEffect
(but that gives me a fat red warning in console) */
props.dispatch({ type: "UPDATE_MESSAGES", payload: updatedMessagesState });
}
});
您的useEffect需要一个依赖项,如果您没有在useEffect中提供依赖项,例如您的情况,它将始终在每个渲染器上运行。在您的useEffect或[]
中提供[any state or prop on which this effect depends]
作为第二个参数。