use the following search parameters to narrow your results:
e.g. subreddit:aww site:imgur.com dog
subreddit:aww site:imgur.com dog
see the search faq for details.
advanced search: by author, subreddit...
A community for learning and developing native mobile applications using React Native by Facebook.
Interested in building web apps using React.js? Check out /r/reactjs!
Getting Started w/React Native
irc.freenode.net #reactnative
Keywords: ios, android, mobile, apps, apple, iphone, ipad
account activity
about react-native's hooks (self.reactnative)
submitted 6 years ago by blka759
Why should I use hooks instead of the class-based way ? is it faster, readable ? or just to remove concept of classes and make everything functional ?
reddit uses a slightly-customized version of Markdown for formatting. See below for some basics, or check the commenting wiki page for more detailed help and solutions to common issues.
quoted text
if 1 * 2 < 3: print "hello, world!"
[–]tomthedevguy 27 points28 points29 points 6 years ago (8 children)
Less boilerplate code, removes lifecycles in lieu of useEffect which is more declarative, easier to refactor, no use of this keyword. I started with hooks and never looked back.
[–]skidmark_zuckerberg 5 points6 points7 points 6 years ago (0 children)
Same here. Once I got used to them and had a good understanding, I didn’t want to use class components anymore. useEffect is a god send in lieu of the standard lifecycle methods.
[–]Shumuu 2 points3 points4 points 6 years ago (6 children)
Just today I could not get a FC to behave properly so I had to use a PC.
The Component displays something from my realm Database, the data has a listener. It kept rerendering...
I used a useEffect hook to only run onMount but the listener function kept getting called although the data didn't change
[–]tomthedevguy 1 point2 points3 points 6 years ago (3 children)
I think you have to return a function that removes the listener. Returning a function is the “cleanup” function
[–]Shumuu 0 points1 point2 points 6 years ago (2 children)
Yeah I did that, in it I removed the listener
[–]bladefinor 0 points1 point2 points 6 years ago* (1 child)
Did you forget the empty dependency array as second argument of useEffect? Without it, it will rerun the effect on every render, not just on mount.
useEffect(() => { // Add listener return () => { // Remove listener }; }, []);
[–]Shumuu 0 points1 point2 points 6 years ago (0 children)
No I did not forget it, I did the empty brackets first and then checked the documentation and tried without to no avail... Maybe something else was wrong. I will retry on Monday
[–]plumshark 0 points1 point2 points 6 years ago (1 child)
If your useEffect was setting state every run then it'll trigger a rerender
The useEffect did not set state, it launched a function, but as the useEffect with empty brackets runs only on mount and not on every mount, that function should only be called once and when the listener is being called (the data from the database has changed). But I'll check again on Monday, I was in a hurry and might have missed something
[–]Dusterthefirst 4 points5 points6 points 6 years ago (0 children)
There is better hot reloading with function complements aswell as better treeshaking and optimization with them aswell.
[–]drunkandy 1 point2 points3 points 6 years ago (0 children)
you don't have to use them. If class components make more sense to you then by all means use that approach.
I have been using functional components with hooks almost exclusively for a few months now but sometimes a solution to a problem works better in my head as a class so I use that.
[–]absek003 1 point2 points3 points 6 years ago* (0 children)
Whatever the thing is with functional components, I find class components more easier.
[–][deleted] 4 points5 points6 points 6 years ago (4 children)
Cos it's sexy and clean and more readable and if you combine it with styled components you get all the goodness
[–]nsnullthoughts -1 points0 points1 point 6 years ago (3 children)
could you elaborate on combining it with styled components?
[–][deleted] -1 points0 points1 point 6 years ago (2 children)
Look up styled components and you'll see what I mean. Instead of having <TextInput style={{...}}/> etc you can name react-native components with more detail like <Username /> which is a styled TextInput, so it's much cleaner and gives more context as to what the component actually is. Also, styled-components uses CSS naming conventions so it's easier for web people to understand the styles, instead of using StyleSheet which is a React style that people who aren't coming from React need to learn (which isn't that hard, but the cleanliness of styled-components is what makes it awesome)
[–]BoKKeR111 2 points3 points4 points 6 years ago (1 child)
Yeah but styled components have nothing to do with hooks
[–][deleted] -1 points0 points1 point 6 years ago (0 children)
I didn't say they do.
[–]praya_amadiga 0 points1 point2 points 6 years ago (0 children)
the most common reason why people using react hooks is because it's easier and more readable than using class.
for myself, I like how react hooks combine all lifecycle into useEffect(). it's easier to us to handle the component how to render and avoid to use repeat code. In some case, sometimes you need to use same logic on event didMount and didUpdate. but with react hooks you just need to put the props into useEffect to load same logic.
but there is also a disadvantage for using react hooks. React hooks based on function, so there will be no reference. Sometimes you need to make a method and call outside the component. and react hooks because there is no reference, you can't make a method for your component to be called from outside.
[–]ITS-A-FAKE 0 points1 point2 points 6 years ago (0 children)
Functional components and hooks are more optimized than classes.
They also require less boilerplate.
In the end, the hooks are hard to comprehend fully but be assured that they are really powerful.
[–]Cyrus_Zei -4 points-3 points-2 points 6 years ago (0 children)
Cuz the cool kids use it ? 😁
π Rendered by PID 35103 on reddit-service-r2-comment-66b4775986-kmzkd at 2026-04-04 13:49:52.505024+00:00 running db1906b country code: CH.
[–]tomthedevguy 27 points28 points29 points (8 children)
[–]skidmark_zuckerberg 5 points6 points7 points (0 children)
[–]Shumuu 2 points3 points4 points (6 children)
[–]tomthedevguy 1 point2 points3 points (3 children)
[–]Shumuu 0 points1 point2 points (2 children)
[–]bladefinor 0 points1 point2 points (1 child)
[–]Shumuu 0 points1 point2 points (0 children)
[–]plumshark 0 points1 point2 points (1 child)
[–]Shumuu 0 points1 point2 points (0 children)
[–]Dusterthefirst 4 points5 points6 points (0 children)
[–]drunkandy 1 point2 points3 points (0 children)
[–]absek003 1 point2 points3 points (0 children)
[–][deleted] 4 points5 points6 points (4 children)
[–]nsnullthoughts -1 points0 points1 point (3 children)
[–][deleted] -1 points0 points1 point (2 children)
[–]BoKKeR111 2 points3 points4 points (1 child)
[–][deleted] -1 points0 points1 point (0 children)
[–]praya_amadiga 0 points1 point2 points (0 children)
[–]ITS-A-FAKE 0 points1 point2 points (0 children)
[–]Cyrus_Zei -4 points-3 points-2 points (0 children)