You probably haven't understood react useEffect yet.
making sense of the useEffect hook and understanding its purpose.

Search for a command to run...
making sense of the useEffect hook and understanding its purpose.

No comments yet. Be the first to comment.
Embark with me on this interesting Journey of how I got to test my react app on my mobile from my local PC environment.

Here is a general overview of what the DNS is and how it operates

get to Know what React Query is all about

A guide to the redux ecosystem with Mawit Gad

Hey there! this article is a follow-up to one of my articles where I describe why the useEffect hook renders twice and how to stop it possibly.
The useEffect hook is a very used and thus popular hook in the react's ecosystem, it serves its purpose when used correctly but can represent a 'foot gun' when used incorrectly and so resulting in a lot of hate from some devs. Actually, the useEffect hook is less of a dangerous hook or tool, and more of a misunderstood one. In order to fully enjoy the potential of this hook, one must understand the initial purpose for which the hook was designed and published.
let's look at an example here. let's say we have an app that makes a connection with a server for a particular component, the connection might need some arguments like some id from the react component, and should always be in sync with these arguments. in order to create this connection and keep it in sync with our app's states, the useEffect hook would be suitable
const server = new Server()
export default function app({id}){
useEffect(()=>{
server.connect(id)
return ()=> server.disconnect()
},[id])
}
notice how I added the id prop to the dependency array so that the connection will always be remade when the id changes and thus always be In sync with the latest Id. Also, notice how I added a clean-up function to maintain consistency and avoid bugs caused by multiple unnecessary connection instances.
You could also use useEffect to fetch data from the server for use in your application, but it is mostly advised to use a framework for that task because while it is possible, the odds of you falling into issues like "race conditions", "untrue dependency array", and other issues are high. some frameworks that might serve the purpose here are,
-react query
-remix
-useSWR()
-and the use() function which is relatively new and cool.
Hopefully, you now understand that the useEffect hook is more for synchronizing than performing effects in our apps. therefore if you are having useEffects all over the place in your code then you are probably using it wrong. Here are some instances where you might be using the useEffect wrongly.
// bad practice
export default function app(){
const [ state , setState ] = useState()
const [ derived , setDerived ] = useState()
useEffect(()=>{
setDerived(state + 1)
},[state])
// app statements continue...
}
// better
export default function app(){
const [ state , setState ] = useState()
const derived = state + 1
// app statements continue...
}
In conclusion to this article, the useEffect hook is a very useful tool that just needs to be understood and used for its purpose. when used correctly, it offers so much value.
so guys those are some insights on the useEffect hook, to continue learning I would advise you to go to the new react docs React Docs Beta, and till next time, happy coding.