useReducer and useEffect dependency #9267
Unanswered
umeshjain1999
asked this question in
Q&A
Replies: 1 comment 1 reply
-
this is a react issue, not a CRA issue. It would be helpful if this was a working (or not-working in this case) demo |
Beta Was this translation helpful? Give feedback.
1 reply
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
-
Code is about fetching random bad joke from api on press of a button
I am see this warning in my console log
./src/App.js
Line 67:7: React Hook useEffect has a missing dependency: 'jokeState'. Either include it or remove the dependency array react-hooks/exhaustive-deps
Problem description
I google a lot 😓 but I couldn't find anything related to useEffect in which we have condition based dependency which is a state from useReducer. I gave the dependency as "jokeState.buttonClick" because I want the component to re-render after every click of button.
Any other way to do it 😍? but not using useState because I want to try with useReducer.
Code here..
`import React , {useEffect , useReducer}from 'react';
import './App.css';
const initialState = {
joke : '',
buttonClick : false,
loading : false,
punchline : true
}
const App = () => {
/useReducer here......./
const reducer = (state , action) => {
switch (action.type) {
case 'FETCHING_JOKE' :
return {...state , joke : action.joke ,
punchline : !state.punchline }
}
}
const [jokeState , dispatch] = useReducer(reducer,initialState);
useEffect(
() => {
)
const jokeApi = async (endpoint) => {
const response = await fetch(endpoint);
const data = response.json();
return data;
}
}
export default App;
`
Thanks in advance 😁🙏
Beta Was this translation helpful? Give feedback.
All reactions