【发布时间】:2021-09-10 01:31:57
【问题描述】:
我有这个组件和如下界面
interface IProps {
data: Array<{
low: number,
high: number,
open: number,
close: number,
openTime: number
}>
}
const Canvas: React.FC<IProps> = (props) => {
const [data, setData] = useState<Array<{
low: number,
high: number,
open: number,
close: number,
openTime: number
}>>();
}
useEffect(() => {
setData(props.data)
}, []) //warning1
useEffect(() => {
const canvas: any = canvasRef.current;
canvas.width = window.innerWidth;
canvas.height = window.innerHeight;
if (data) {
setCandleWidth(canvas.width / data.length)
}
}, [data && data.length != 0]) //Warning2
}
两个useeffects都给我警告
第一个就是这样,因为我使用它就好像它是一个 componentDidMount() ,但它说
React Hook useEffect 缺少一个依赖项:'props.data'。任何一个 包含它或删除依赖数组。如果“setData”需要 'props.data' 的当前值,也可以切换到 useReducer 而不是 useState 并在 reducer 中读取“props.data”。
第二个说如下,而我只是想在设置数据后触发它
React Hook useEffect 缺少一个依赖项:'data'。要么包括 它或删除依赖 array.eslintreact-hooks/exhaustive-deps React Hook useEffect 在依赖数组中有一个复杂的表达式。 将其提取到一个单独的变量中,以便它可以是静态的 checked.eslintreact-hooks/exhaustive-deps
那么有什么更好的方法来做到这一点?
【问题讨论】:
标签: reactjs typescript