[英]how to fix useEffect has a missing dependency "Dispatch"
Hello I have a problem with my eslint and I'm not sure how to respond with my dispatch's:你好,我的 eslint 有问题,我不知道如何回复我的派遣:
const { messages } = ChatReducer;
const [isTyping, setIsTyping] = useState(false);
console.log(isTyping)
useEffect(() => {
if (messages[messages.length - 1].type === 'bot') {
setIsTyping(true);
const timeoutId = setTimeout(() => {
setIsTyping(false);
dispatch(wait_end());
}, 3000);
}
}, [messages]);
useEffect(() =>
dispatch(answer_Message(['hi 😃','hi two'])
), []);
return (
<>
{messages.map((message, index) => (
<>
{message.text ? (
<Styled.MessageFlexColumn ref={messagesEndRef} key={index}>
{message.type === 'user' ? (
<UserText key={index}>{message.text}</UserText>
) : (
<BotText
key={index}
isTyping={isTyping && index === messages.length - 1}
>
{message.text}
</BotText>
)}
<Styled.Status />
</Styled.MessageFlexColumn>
) : (
''
)}
<div ref={messagesEndRef} />
</>
))}
</>
)
action:行动:
export const checkMessage = text => {
return dispatch => {
dispatch(sendMessage(text));
dispatch(wait_anwser());
dispatch(botMessage(verify(text)));
};
};
export const answer_Message = text => {
return dispatch => {
text.map((message, index) => {
dispatch(botMessage(message));
})
}
}
Well I also have a problem with my useEffect and my state I'm basically using it to send an array of initial messages to display in my chat:好吧,我的 useEffect 和我的状态也有问题,我基本上是用它来发送一组初始消息以显示在我的聊天中:
useEffect(() =>
dispatch(answer_Message(['hi 😃','hi two'])
), []);
The problem is that when sending 2 texts and my isTyping state is true only once even being 2 texts and not enter on my if:问题是,当发送 2 条文本时,我的 isTyping 状态只为一次,即使是 2 条文本,也不会输入我的 if:
if (messages[messages.length - 1].type === 'bot'
if (messages[messages.length - 1].type === 'bot'
alert eslint:警报 eslint:
Line 70:6: React Hook useEffect has a missing dependency: 'dispatch'. Either include it or remove the dependency array
Line 74:6: React Hook useEffect has a missing dependency: 'dispatch'. Either include it or remove the dependency array
我认为您不需要在函数调度时调度它,或者您可以尝试将调度添加为[dispatch]
的依赖项
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.