我有一个带有 useEffect 钩子的 react 组件,如下所示:
const LocationEditor: React.FC<SectionEditorProps> = (props) => {
const [section, setSection] = useState({...(props.section as Location)});
useEffect(() => {
props.onChange(section);
}, [section]);
Run Code Online (Sandbox Code Playgroud)
我在运行项目时收到此警告:
React Hook useEffect has a missing dependency: 'props'. Either include it or remove the dependency array. However, 'props' will change when *any* prop changes, so the preferred fix is to destructure the 'props' object outside of the useEffect call and refer to those specific props inside useEffect react-hooks/exhaustive-deps
Run Code Online (Sandbox Code Playgroud)
所以我尝试将组件更改为如下所示,解构道具:
const LocationEditor: React.FC<SectionEditorProps> = ({section, …Run Code Online (Sandbox Code Playgroud)