This post has been de-listed
It is no longer included in search results and normal feeds (front page, hot posts, subreddit posts, etc). It remains visible only via the author's post history.
So I can fetch data on mount with no problem .. although I am curious.
what if the fetch api is updated on the backend.. how will my page re render and set the new data if I have ZERO dependencies like below ? Or does useEffect know to automatically update if the api is updated on the backend?
(using react version 16)
example below.
const [data, setData] = useState({})
useEffect(() => {
fetch('some/url')
.then((response) => response.json())
.then((res) => setData(res));
}, [])
Also I've tried adding a dependency assuring that my data updates but now my useffect calls twice on mount..idk why.
const [data, setData] = useState({})
useEffect(() => {
fetch('some/url')
.then((response) => response.json())
.then((res) => setData(res));
}, [data])
bottom line is, I want to have the fetch api with ZERO dependencies so that it will not render twice but still be able to update on api change. I just need it to mount once but update when it needs to . I need to be able to post to the backend and see the data show up immediately
Separate question: Why does useEffect with a dependency render twice ? and not once?
Subreddit
Post Details
- Posted
- 2 years ago
- Reddit URL
- View post on reddit.com
- External URL
- reddit.com/r/webdev/comm...