简体   繁体   English

如何修复 useEffect 缺少依赖项“Dispatch”

[英]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.

相关问题 useEffect 缺少依赖项:'dispatch' - useEffect has a missing dependency: 'dispatch' 如何修复 React Redux 和 React Hook useEffect 缺少依赖项:'dispatch' - How to fix React Redux and React Hook useEffect has a missing dependency: 'dispatch' React Hook useEffect 缺少依赖项:'dispatch' - React Hook useEffect has a missing dependency: 'dispatch' React Hook useEffect 缺少依赖项:&#39;notes&#39;,如何修复? - React Hook useEffect has a missing dependency: 'notes', how to fix it? 如何修复 React Hook useEffect 缺少依赖项 - How to fix React Hook useEffect has a missing dependency 如何修复这个“React Hook useEffect has a missing dependency”警告? - How to fix this “React Hook useEffect has a missing dependency” warning? 如何在自定义挂钩中修复“useEffect 缺少依赖项” - How do I fix “useEffect has a missing dependency” in custom hook 如何解决此警告:“React Hook useEffect 缺少依赖项:'history'”? - How to fix this warning: “React Hook useEffect has a missing dependency: 'history'”? 修复 React Hook useEffect 缺少依赖项 - Fix React Hook useEffect has a missing dependency 如何解决“React Hook useEffect 缺少依赖项。包含它或删除依赖项数组”的问题? - How to fix "React Hook useEffect has a missing dependency. Either include it or remove the dependency array" problem?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM