News for hackers. Stuff that matters.
Re:Common mistakes writing React components with hooks by pg_bot on Tuesday July 21, 2020 @09:54AM (#23906792)
I would argue that writing a React component with a hook is a mistake. There is usually an easier/clearer way to solve the problems that hooks are intended to solve with the existing React primitives.
Re:Common mistakes writing React components with hooks by qudat on Tuesday July 21, 2020 @10:37AM (#23907105)
I get what you're saying but disagree. Hooks are essential and make life much easier for the react developer, especially when using something like `react-redux` or `react-router`. Prop-drilling or HoC might seem like a better design until you actually have to work in a system that leverages them and realize it's an indirection nightmare.
Re:Common mistakes writing React components with hooks by pg_bot on Tuesday July 21, 2020 @11:30AM (#23907600)
I would go a step further and say that React Components should not manage their state internally. (this.state was a mistake to add to the project) You can create every single UI in the world using pure functional components. They are easier to understand since they act like every other function, and they are simpler to test.

Keep your state externally and just grab what you need on each subcomponent with the `connect` function from `react-redux`. Easy peasy front-end development.

Re:Common mistakes writing React components with hooks by LorenzA on Tuesday July 21, 2020 @12:12PM (#23908053)
not sure i understand you correctly, but keeping all state external just clutters up the store i feel. if the state is not used anywhere else and you also don't need to change it externally i would not move state out of the component. also if your state is always outside of your component they will only be reusable if you hook up the store in all your projects the same way
Re:Common mistakes writing React components with hooks by pg_bot on Tuesday July 21, 2020 @12:42PM (#23908367)
Nah you can get reusability by creating shared components that you just pass props into.

For example, you can have a date picker that just accepts variables to display the current state and parent component that reads data from the store and passes it into the date picker component.

This method is also great for debugging, since you can just replay the state transitions over again if there is an application error. If the component holds the state, then it can be cumbersome to reproduce the error.

Re:Common mistakes writing React components with hooks by LorenzA on Wednesday July 22, 2020 @03:51AM (#23914870)
a date picker would be super annoying to use if you would have all the state in the store. that are probably 40 variables. assume you open the date picker can can move threw the tabs of the month and you are not interested in changing this from outside.

if you have no state in the component that would not be possible. you can also not wrap this with an other component that can hold this state because you don't want components with state. that means all behaviour that requires state would have the state in the store this will just be crazy

Re:Common mistakes writing React components with hooks by pg_bot on Wednesday July 22, 2020 @09:40PM (#23923368)
You can do it in 3 stored variables
Re:Common mistakes writing React components with hooks by robjan on Tuesday July 21, 2020 @12:38PM (#23908329)
Redux is global state. Adding all local state to a global store will result in a complete mess for any non-trivial app.
Re:Common mistakes writing React components with hooks by pg_bot on Tuesday July 21, 2020 @12:50PM (#23908458)
Maybe if you're writing it ;)

Most of the time it mirrors the structure of a file system which is the exact way (almost) every program is written.

As with most software projects, keeping organized is like 80% of the battle.

Re:Common mistakes writing React components with hooks by jrvidal on Tuesday July 21, 2020 @11:07AM (#23907385)
Pardon my ignorance, but is prop drilling a problem once you've bought into Redux or similar?
Re:Common mistakes writing React components with hooks by kls on Wednesday July 22, 2020 @07:20PM (#23922630)
No it provides helper functions that inject props into your component.
Re:Common mistakes writing React components with hooks by duxup on Tuesday July 21, 2020 @09:57AM (#23906809)
Can you explain what you mean by that? I'm honestly a little lost on what you're saying but I am curious.
Re:Common mistakes writing React components with hooks by dgritsko on Tuesday July 21, 2020 @10:59AM (#23907318)
I read it as "I don't like hooks."
Re:Common mistakes writing React components with hooks by duxup on Tuesday July 21, 2020 @11:01AM (#23907331)
I wondered the same but the text was also confusing so I thought maybe there was something else there.

At least for me hooks dramatically clean up the code.

Outside of error boundaries (I think those still have to be classes) any new component for me is a function component and if it needs state, has hooks.

Granted I still maintain a lot of class components back from before the days of hooks.

Re:Common mistakes writing React components with hooks by esquevin on Tuesday July 21, 2020 @10:03AM (#23906849)
Exemples would be welcome here as I've a strong belief that hooks made react components cleaner / shorter
Re:Common mistakes writing React components with hooks by flowerlad on Tuesday July 21, 2020 @10:29AM (#23907046)
For one thing you may not use the ref attribute on function components because they don’t have instances. That means the component can't have a .focus() method for example.

For simple components hooks may be fine. For more complex ones I prefer classes.

Re:Common mistakes writing React components with hooks by Octoth0rpe on Tuesday July 21, 2020 @10:34AM (#23907079)
I believe the useRef hook is used for this problem:

https://reactjs.org/docs/hooks-reference.html#useref

The example even uses .focus()

Re:Common mistakes writing React components with hooks by cnity on Tuesday July 21, 2020 @10:42AM (#23907143)
Re:Common mistakes writing React components with hooks by avery42 on Tuesday July 21, 2020 @10:52AM (#23907251)
Maybe I misunderstood, but isn't this what `React.forwardRef` is for?
Re:Common mistakes writing React components with hooks by evan_ on Tuesday July 21, 2020 @12:44PM (#23908393)
along with useImperativeHandle for writing your own imperative functions like focus() or whatever
Re:Common mistakes writing React components with hooks by kelchm on Tuesday July 21, 2020 @11:22AM (#23907519)
While I don't agree that writing a component with a hook is always a mistake, I have seen them used in situations where it wasn't warranted.

In the end, they are just another tool -- knowing how to use the tool appropriately is the critical part.

Re:Common mistakes writing React components with hooks by fendy3002 on Tuesday July 21, 2020 @10:24AM (#23907009)
I haven't use hooks and don't prefer it, however I must say that there isn't alternative to hooks that is shorter in syntax, and maybe easier.
Re:Common mistakes writing React components with hooks by jordanwallwork on Tuesday July 21, 2020 @10:51AM (#23907228)
Yeah I wasn't sold on hooks at first, mainly because I was having to re-think stuff that I already knew, but man now I'm used to them I'm so much more productive and I absolutely love it
Re:Common mistakes writing React components with hooks by WesleyJohnson on Tuesday July 21, 2020 @02:30PM (#23909444)
I echo this statement. I use Django a lot for my backends and I was very, very against moving to class-based views. I'm not sure why, I love OOP, but I was so used to function-based views. I finally made the switch (well, I use both when appropriate) and I love them.

During this time, I started playing around with React and I thought, I learned my lesson, it's class-based components all the way. Yet again, the community started going to "other" way with function-based components and I was steadfast in my attachment to classes - refusing to budge.

Of course, I eventually made the switch and wish I had done so sooner. Once you get the hang of hooks, they're so much easier to reason about and, as you said, make me much more productive.

Re:Common mistakes writing React components with hooks by fendy3002 on Tuesday July 21, 2020 @07:09PM (#23911897)
That's not without reason, javascript class is infamous for being weird with 'this' gotchas, and class is 2nd class citizen there (not as good / powerful as one in java). Meanwhile function based view is neat.

However without hooks, only class based component has side effects and lifecycle, so you like it, then made switch to hooks because it's neat.