useEffect
is a hook in React that allows you to perform side effects in function components. This hook can be used to fetch data, set up subscriptions, and manually trigger re-renders, among other things. When you call useEffect
, you pass in a function that will be executed after the component has rendered. You can also provide an array of dependencies, so that the effect will only run if any of those dependencies change. Here's an example of useEffect
in TypeScript:
index.ts493 chars18 lines
In this example, the useEffect
hook is being used to log a message after rendering the component. The empty dependency array []
indicates that the effect should only run once (equivalent to componentDidMount
in class components).
gistlibby LogSnag