React钩子覆盖了对象数组

时间:2019-09-03 13:12:56

标签: node.js reactjs react-hooks wit.ai

我正在尝试使用React钩子和Wit.ai对chatbot接口进行编码。

我尝试强制设置消息(setMessages([... messages,currentValue]),但这也不起作用。这是代码:

const [currentValue, setCurrentValue] = useState('');
const [messages, setMessages] = useState([]);


const handleChange = (event) => {
    setCurrentValue(event.target.value); // handling input change
}

const sendMessage = (event) => {
    event.preventDefault();
    if (currentValue.trim() !== '' || currentValue.trim().length > 1) {
        witClient.message(currentValue).then(data => {
            setMessages(() => [...messages, {text: currentValue, sender: 'user'}]); // here i set the user message
            if (data.entities) {
                setMessages(() => [...messages, {text: 'message from bot', sender: 'bot'}]); // this line seems to overwrite the user message with the bot message
                setCurrentValue('');
            }
        });
    }
    document.querySelector('input').focus();
}

当我处理漫游器响应时,它会覆盖用户消息。

2 个答案:

答案 0 :(得分:1)

由于您依赖先前的值,因此可以使用功能模式来设置状态,如下所示:

文档:https://reactjs.org/docs/hooks-reference.html#functional-updates

        setMessages((priorMessages) => [...priorMessages, {text: currentValue, sender: 'user'}]);
======================================
        if (data.entities) {
            setMessages((priorMessages) => [...priorMessages, {text: 'message from bot', sender: 'bot'}]);

答案 1 :(得分:0)

当您在messages之后访问if statement时,实际上覆盖了第一个更改,原因[...messages, {text: currentValue, sender: 'user'}]仅反映在下一个render中。一次设置所有更改,以防止发生这种情况

const sendMessage = (event) => {
    event.preventDefault();
    if (currentValue.trim() !== '' || currentValue.trim().length > 1) {
        witClient.message(currentValue).then(data => {
            let newMessages = [...messages, {text: currentValue, sender: 'user'}]
            if (data.entities) {
                newMessages = newMessages.concat({text: 'message from bot', sender: 'bot'})
                setCurrentValue('');
            }
            setMessages(messages)
        });
    }
    document.querySelector('input').focus();
}