I'm getting the error "React Hook React.useEffect has a missing dependency: 'openPosts'" where openPosts is state in a 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]);
I've read around but I haven't understood why I'm getting this error. Can I ignore it?
I essentially want state to filter as above with a change in props.userId and thought this would be a clean way to do it. I could also create more state to track any change in props.userId but if the above can work I'd prefer it.
openPostschanging if/whenuserIdchanges? By not includingopenPostsas a dependency ofuseEffectit simply means that ifopenPostschanges then youruseEffectcode won't re-run unlessuserIdis also updated.openPostsis updated each timeuserIdchanges. The behaviour you described is intentional.