how does swr work with useEffect??request many times when props change.
See original GitHub issueit‘s my first time to use this library. useSWR('/api/data', fetchData)
will excute when App props changes. how can i use useEffect let it only work in componentDidMount…it seems not work in useEffect.
import useSWR from 'swr'
import fetchData from './fetch-data'
function App () {
const { data } = useSWR('/api/data', fetchData)
// ...
}`
expect
`import useSWR from 'swr'
import fetchData from './fetch-data'
function App () {
useEffect(() => {
const { data } = useSWR('/api/data', fetchData)
// ...
}, [])
}
Issue Analytics
- State:
- Created 4 years ago
- Comments:8 (4 by maintainers)
Top Results From Across the Web
use SWR with depending request data - reactjs - Stack Overflow
This is because a re-render will always happen when user changes from undefined to some data. You can use this method to fetch...
Read more >Getting Started - SWR
SWR is a React Hooks library for data fetching. SWR first returns the data from cache (stale), then sends the fetch request (revalidate),...
Read more >Performance - SWR - Vercel
For example, an app that renders the current user's avatar 5 times: ... If the data value isn't changed, a re-render will not...
Read more >Mutation & Revalidation - SWR
In many cases, applying local mutations to data is a good way to make changes feel faster — no need to wait for...
Read more >Arguments - SWR
Multiple Arguments# ... This is incorrect. Because the identifier (also the cache key) of the data is '/api/user' , even if token changes,...
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
You can’t call hooks inside
useEffect
: https://reactjs.org/docs/hooks-rules.html#only-call-hooks-at-the-top-levelInstead you can do this:
When
key
isnull
, SWR will pause itself. It’s also explained here https://github.com/zeit/swr#key-as-a-function.@corysimmons you can just remove that
useEffect
: