-
Notifications
You must be signed in to change notification settings - Fork 1.2k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
how does swr work with useEffect??request many times when props change. #77
Comments
You can't call hooks inside Instead you can do this: function App () {
const [mounted, setMounted] = useState(false)
const { data } = useSWR(mounted ? '/api/data' : null, fetchData)
useEffect(() => {
setMounted(true)
}, [])
} When |
@quietshu Maybe I'm missing the intent here, but wouldn't fetch happen on every rerender this way? (since |
@moroshko nope, SWR isn't like the normal |
@shuding My fetched URL changes depending on some That is... how can I accomplish something like below if I can't nest const [selectOption, setSelectOption] = useState()
useEffect(() => {
useSWR(`https://api.example.com/${selectOption}`) // 🚨 can't nest hooks
}, [selectOption])
return (
<select>...</select>
) |
@corysimmons you can just remove that const [selectOption, setSelectOption] = useState()
useSWR(`https://api.example.com/${selectOption}`)
return (
<select>...</select>
) |
@shuding Awesome. btw I like your Github quote and domain name. Thanks for the nice library! |
I'm having this same issue. I'm using the setMounted technique shown above. However, I still see network requests to my SWR endpoint when I change another property on the component and render gets called. Maybe SWR always makes the call in DEV mode? If I add revalidateOnFocus: false to the SWR request then it seems to work. I'm testing this in Chrome developer tools. I wonder if it's doing something funky. |
RevalidateOnFocus make SWR run a new fetch every time the tab recover focus, when you focus on the browser DevTools the tab lose focus, when you click again in the page it will recover the focus and revalidate the data triggering a new request. |
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.The text was updated successfully, but these errors were encountered: