Live data from Hacker News

Common mistakes writing React components with hooks

lorenzweiss.de

61–70 of 98 posts

Re: Common mistakes writing React components with hooks

#61
post #60

I like hooks but here are some uncertainties that bother me. I am quiet new to React and hooks, but I am a senior dev, so I am wondering if any of this confuses others. * I sometimes have to use an empty dependency array inside useEffect to run something only when component first loads. The linter yells at me, but it makes sense. I once read an article which told to put things in functions and wrap those functions in…

Same with the empty array

Re: Common mistakes writing React components with hooks

#62
post #60

I like hooks but here are some uncertainties that bother me. I am quiet new to React and hooks, but I am a senior dev, so I am wondering if any of this confuses others. * I sometimes have to use an empty dependency array inside useEffect to run something only when component first loads. The linter yells at me, but it makes sense. I once read an article which told to put things in functions and wrap those functions in…

I would ignore whoever told you not to use an empty array for the dependency argument. Change your linter rules. It's fine.

Re: Common mistakes writing React components with hooks

#63
post #52
post #33

Earlier quoted context omitted.

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.

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…

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

#64
post #60

I like hooks but here are some uncertainties that bother me. I am quiet new to React and hooks, but I am a senior dev, so I am wondering if any of this confuses others. * I sometimes have to use an empty dependency array inside useEffect to run something only when component first loads. The linter yells at me, but it makes sense. I once read an article which told to put things in functions and wrap those functions in…

If you need an effect to run before the component draws, try `useLayoutEffect` instead of `useEffect`. https://reactjs.org/docs/hooks-reference.html#uselayouteffec...

Re: Common mistakes writing React components with hooks

#65
post #57
post #52

Earlier quoted context omitted.

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…

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

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

#66
post #38

Earlier quoted context omitted.

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.

Maybe I misunderstood, but isn't this what `React.forwardRef` is for?

along with useImperativeHandle for writing your own imperative functions like focus() or whatever

Re: Common mistakes writing React components with hooks

#67
post #62
post #60

I like hooks but here are some uncertainties that bother me. I am quiet new to React and hooks, but I am a senior dev, so I am wondering if any of this confuses others. * I sometimes have to use an empty dependency array inside useEffect to run something only when component first loads. The linter yells at me, but it makes sense. I once read an article which told to put things in functions and wrap those functions in…

I would ignore whoever told you not to use an empty array for the dependency argument. Change your linter rules. It's fine.

That runs the risk of someone less experienced introducing a bug later when a new dependency is introduced. I think it's better to simply do whatever the linter says.

Re: Common mistakes writing React components with hooks

#68
post #63
post #52

Earlier quoted context omitted.

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…

Redux is global state. Adding all local state to a global store will result in a complete mess for any non-trivial app.

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

#69
post #60

I like hooks but here are some uncertainties that bother me. I am quiet new to React and hooks, but I am a senior dev, so I am wondering if any of this confuses others. * I sometimes have to use an empty dependency array inside useEffect to run something only when component first loads. The linter yells at me, but it makes sense. I once read an article which told to put things in functions and wrap those functions in…

If the dependency array lint rule is yelling at you there's a very, very good chance your component is broken. As an example I used to see class components like this all the time:

   class Foo extends React.Component {
      componentDidMount() {
         fetchFoo(this.props.id)
            .then(foo => this.setState({foo}))
      }
      render() { ... }
   }
This component is fundamentally broken because there is no `componentDidUpdate` to re-call `fetchFoo` when `this.props.id` changes. I could easily be rendering a new id with an old id's foo. The developer is making an assumption that the initial value of `id` will never change, therefore making assumptions about how the parent component is instantiating `Foo`. Rewriting this using hooks makes this immediately obvious:

   const Foo = ({id}) => {
      const [foo, setFoo] = React.useState(null)
      React.useEfect(() => {
         fetchFoo(id).then(setFoo)
      , []) // Linter complains, and it should!
      return ...
   }
There's nothing wrong with using the empty array as your deps so long as it's actually what you want to do. If `fetchFoo()` didn't require any arguments and we only wanted to invoke it once [] would absolutely be the right dependency array. There are few cases I can think of where disabling the linter check is correct. The most common occurence in my code is if the effect uses a value not required to be fresh: e.g. something used for an optimization.

Re: Common mistakes writing React components with hooks

#70

The first one just feels like a premature optimization. Yes calling setCount forces a rerender of that component, but unless there's lots of subcomponents inside that component, I wouldn't bother. Chances are later you'll need that state in the view, and if you have "unexpected side effects" from rerendering then that is the problem. The other tips are fine; effects should have a single responsibility and links and b…

I have a really large chat app with thousands of users and I realized the other day that I accidentally re-render the root component on _every keystroke_. Still, it's not a disaster and I haven't bothered to fix it because no-one notices.
Post reply on HN