Skip to content

Make it clearer how to run an effect only once (componentDidMount style) #1930

Description

@hedgerh

Based on this conversation on Twitter, it's perhaps not clear enough how to make an effect hook only run once on mount (a la componentDidMount.)

The relevant info on how to do it properly is tucked away near the bottom of the effect hook doc, in a footnote of this tip. This is a major use case that should probably have higher priority in the documentation. I imagine a vast majority of apps actually need to do use an effect in this manner.

I assume people are probably doing something like

function MyComponent({ fetchData, data }) {
  useEffect(() => {
    if (!data) {
      fetchData()
    }
  })
}

I can make the change to the docs, but where's the best place to put it? Should I make it a tip? Does it deserve higher priority than that?

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions