How do Hooks work in programming and in React?
Hello, can someone explain what are hooks in programming, more specifically in React please, what do they do?
8 Replies
How was it initially explained to you? What didn’t make sense?
Have you read the official docs on the topic? If yes, what didn’t make sense?
hmm from what I have understood, it's a way of how our program can call another program or the other way round where our program gets called by another program. But I didn't really understood how can this be done
I know hooks are more about states though
how we implement them though seems a bit difficult
So you know what they are, but not how they work? Because that’s not what your title says.
A good title with supporting facts makes it easier for others to help. In this case, I don’t use React so I can’t help any further. But hopefully someone who does can read your more specific query and chime in
Hooks are just functions that allow you to 'hook in' to React's lifecycle: https://react.dev/reference/react/hooks
Adding onto lifecycle: I'm not a React developer & can't find the page I was reading for interview prep a few months ago but you can take a look at this to better understand the React lifecycle:
Every React component goes through the same lifecycle: A component mounts when it’s added to the screen. A component updates when it receives new props or state, usually in response to an interaction. A component unmounts when it’s removed from the screen.This isn't the specific page I was reading months earlier, but this is where it's referenced: https://react.dev/learn/lifecycle-of-reactive-effects#the-lifecycle-of-an-effect There's a lot of content / videos on this so feel free to do your own research on it too You can also build your own custom hooks that leverages React's hooks 🙂
A hook is basically a re-usable function that you can use in your functional components. It can have it's own state, logic, etc. I typically use hooks for contexts, e.g.:
And then inside a component:
yep sorry, will adjust the title
Hooks are pretty much just a way to get a component's state out of the function and into some global state. The function has no way to remember state without them
Your question is very broad though, do you have any more specific questions?
not yet, will come back with more questions as I progress in learning React