26
loading...
This website collects cookies to deliver better user experience
useEffect
hook. It calls the function you pass it each time the component renders or updates. Through this article, we’ll learn why React implements this functionality. useEffect
hook to start fetching the needed data from the server. In the meanwhile, the component renders a placeholder output. You know what it is.By using this Hook, you tell React that your component needs to do something after render. React will remember the function you passed (we’ll refer to it as our “effect”), and call it later after performing the DOM updates.
useEffect
, and it remembers to call it after each render or update. This means that by the time the component calls your effect, it has already updated the state. And the browser has finished painting the screen.useEffect
hook.const Drapery = (props) => {
const [isLoading, setIsLoading] = useState(true)
const [data, setData] = useState(null)
useEffect(() => {
fetch("https://example.com/endpoint")
.then(res => res.json())
.then(data => {
setData(data)
setIsLoading(false)
})
.catch(error => console.log("There was an error while fetching data", error)
}, [])
return isLoading ?
<Spinner /> :
<ListComponent data={data} />
}
loading
. In case its value is true, it means data isn't available. So the component should render a loading indicator. The other one is data
which holds a list of drapery items. And ListComponent
is going to render them.useEffect
part. The hook accepts two arguments; a function and a dependency array. React
will start to track. Anytime any of the values in this array changes, React calls your effect.useEffect
tries to start fetching data from the server. JS fetch API
returns a promise. We use it to update the states as soon as we get the response.data
state with what we get from the server. Then we set the loading
state to false
. Then the condition in our component's return
expression evaluates to false
. That’s when it renders the data using ListComponent
.effect
causes a re-render by updating states, it doesn't affect the render. It runs in parallel. And it never causes any direct changes in what the component renders.localStorage API
is a good option for this case.useEffect
hook to listen to changes in state as soon as it is updated. Then we can set a JSON clone of the new state in the client's storage.const UserContext = createContext()
const UserProvider = ({children}) => {
const [user, setUser] = useState(null)
useEffect(() => {
const persistedUserData = localStorage.getItem("user")
if(persistedUserData) setUser(JSON.parse(persistedUserData))
}, [])
useEffect(() => {
const jsonUserData = JSON.stringify(user)
localStorage.setItem("user", jsonUserData)
}, [user])
return (
<UserContext.Provider value={{user}}>
{children}
</UserContext.Provider>
)
}
React.createContext
to initialize a context. If you don't know about context
, you can think of it as an API. It makes the value you pass to its Provider
available to every component in its tree. To learn more you can read the documentation.Provider
, we have used three hooks. One is a useState
which holds user info. Its initial value is null, which means no user data is available.useEffects
. The first effect runs only after the first render, as its dependency array is empty. We use it to check if there's any user data previously stored in the client's storage. If so, we parse the JSON
data as a JS
object and update our state with it.user
state value. So it will run every time we update the user state. This effect makes a JSON
copy of the user
state and saves it to the client's localStorage
.useEffect
is a good example of when we should use effects. We update the client's localStorage, which causes no change in the render. There's no place for its logic in the component body.useEffect
like the example below:const Editor = (props) => {
useEffect(() => {
window.addEventListener("beforeunload", showDialog)
return () => window.removeEventListener("beforeunload")
}, [])
return <EditorPanel/>
}
window
's beforeunload
event. This event is fired whenever the user tries to close the browser or the current tab.useEffect
hook is a good choice in this case because the component is already rendered. Then we're sure that the window object is loaded and we can attach an event listener
to it.return
statement in the useEffect
hook. You can always return a function in your effect. React will call this function when the component is unmounted.cleanup
function. Since effects like this listener are out of the component's scope and are not related to its render, it's always a good idea to write a cleanup function for them.