使用useRef来存储输入框的值,并在AutoComplete的onChange函数中将value设置为useRef的值,而不是直接将其设置为useState。示例如下:
import React, { useState, useRef } from "react";
import AutoComplete from "./AutoComplete";
function App() {
const [name, setName] = useState("");
const inputRef = useRef("");
const handleAutoCompleteChange = (value) => {
inputRef.current = value;
setName(value);
};
return (
Name: {name}
);
}
export default App;