@salman_arfat
Add an input through which the users can search a database of cities.
getCities(query: string) => Promise(string[])
Note
Write the data fetching logic in the component itself
Potential Problems
What's an effect?
Very simply, an effect is (possible) change in the environment of the program.
}
side effects
Accepts a function that contains imperative, possibly effectful code.
Initial render
Change?
render
Change?
render
Change?
render
Initial render
Change?
Effects
render
Effects?
componentDidMount
componentDidUpdate
Change?
render
Effects?
componentDidUpdate
Put the data fetching logic in a useEffect callback
Potential Problems
Solution? The second argument
https://ncoughlin.com/posts/react-hooks-use-effect/
https://dmitripavlutin.com/react-useeffect-infinite-loop/
Potential Problems
Multiple in-flight request?
The function passed to `useEffect` may return a clean-up function.
Initial render
cleanup?
Effects
render
Effects?
componentDidMount
componentDidUpdate
Change?
render
Effects?
componentDidUpdate
Change?
cleanup?
cleanup?
componentWillUnmount
https://tusharsharma.dev/posts/react-hooks-mental-model
Use the `cleanup` function to invalidate in-flight requests