My react app gives me this warning:
React Hook useEffect has a missing dependency Either include it or remove the dependency array react-hooks/exhaustive-deps
If I move my Stateful components to the inside of the useEffect hook, then my Buttons can't access setButton anymore. Is there any way to fix this issue?
const [value, setValue] = useState([])
const [isLoading, setLoading] = useState(false)
const [buttonValue, setButton] = useState(false)
This is my useEffect Hook
useEffect(() => {
axios.get('http://localhost:5000/').then(res => setValue(res.data))
if (buttonValue === 'delete') {
setLoading(true)
axios.delete('http://localhost:5000/delete').then(() => {
setLoading(false)
setButton(null)
})
} else if (buttonValue === 'create') {
setLoading(true)
axios.post('http://localhost:5000/').then(() => {
setLoading(false)
setButton(null)
})
}
}, [isLoading])
These are my Buttons
<Form>
<Form.Group>
<Button variant='success' className='button' onClick={() => setButton('create')} id='create'>Create Sales Order</Button>
</Form.Group>
<Form.Group>
<Button variant='danger' className='button' onClick={() => setButton('delete')} id='delete'>Delete Sales Order</Button>
</Form.Group>
</Form>
buttonValueto the dependency array. With the current setup the effect will only run on mount, and whenisLoadingchanges. But since you're usingbuttonValuewithin the hook, it is warning you that you should include it as a dependency. - Brian Thompson