我收到错误“React Hook React.useEffect 缺少依赖项:'openPosts'”,其中 openPosts 是 Hook 中的状态。
export const Posts = (props) => {
const [openPosts, setOpenPosts] = React.useState([]); *openPosts is an array of numbers*
React.useEffect(_ => {
let filteredOpenPosts = openPosts.filter(num => num >= 1);
setOpenPosts(filteredOpenPosts);
}, [props.userId]);
我已经阅读过,但我不明白为什么我会收到这个错误。我可以忽略它吗?
我本质上希望通过更改来过滤状态,props.userId
并认为这将是一种干净的方式。我还可以创建更多状态来跟踪任何变化,props.userId
但如果上述方法可行,我更喜欢它。