question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

how does swr work with useEffect??request many times when props change.

See original GitHub issue

it‘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:closed
  • Created 4 years ago
  • Comments:8 (4 by maintainers)

github_iconTop GitHub Comments

72reactions
shudingcommented, Nov 5, 2019

You can’t call hooks inside useEffect: https://reactjs.org/docs/hooks-rules.html#only-call-hooks-at-the-top-level

Instead you can do this:

function App () {
  const [mounted, setMounted] = useState(false)
  const { data } = useSWR(mounted ? '/api/data' : null, fetchData)

  useEffect(() => {
    setMounted(true)
  }, [])
}

When key is null, SWR will pause itself. It’s also explained here https://github.com/zeit/swr#key-as-a-function.

14reactions
shudingcommented, Jun 22, 2020

@corysimmons you can just remove that useEffect:

const [selectOption, setSelectOption] = useState()
useSWR(`https://api.example.com/${selectOption}`)

return (
  <select>...</select>
)
Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found