import React, { useState, useEffect } from 'react';
import Form from 'react-bootstrap/Form';
function Switch(props) {
const [enabled, setEnabled] = useState(false);
useEffect(() => {
if(enabled !== props.enabled) {
setEnabled(props.enabled);
}
}, [props.enabled]);
const handleChange = () => {
props.handleClusterEnabledChange(!enabled);
setEnabled(!enabled);
}
return (
<Form.Check
type="switch"
id={props.id}
label=""
onChange={handleChange}
checked={enabled}
/>
);
}
export default Switch;
错误:
./ src / containers / administration / dwConnections / switch.js 第11:6行:React Hook useEffect缺少依赖项:“已启用”。要么包含它,要么删除依赖项数组react-hooks / exhaustive-deps
答案 0 :(得分:0)
只需添加enabled
作为依赖项:
useEffect(() => {
if(enabled !== props.enabled) {
setEnabled(props.enabled);
}
}, [enabled, setEnabled, props.enabled]);