A component needs to fetch data from an API only once when it's first rendered. Which hook/method can be used to achieve this in a functional component?
- componentDidMount()
- componentWillMount()
- useEffect() with an empty dependency array
- useFetchEffect()
To fetch data from an API only once when a functional component is first rendered, you should use the useEffect() hook with an empty dependency array []. This ensures that the effect runs only once, mimicking the behavior of componentDidMount() in class components.
Loading...