Skip to content

Commit

Permalink
fix(examples): examples/arco-pro useEffect does not clear side effe…
Browse files Browse the repository at this point in the history
…cts (#1283)

* fix: `examples/arco-pro` useEffect does not clear side effects

* changeset ` @farmfe-examples/arco-design-pro` patch

---------

Co-authored-by: ADNY <66500121+ErKeLost@users.noreply.github.com>
  • Loading branch information
RSS1102 and ErKeLost committed May 6, 2024
1 parent e5868af commit 673e844
Show file tree
Hide file tree
Showing 5 changed files with 55 additions and 14 deletions.
5 changes: 5 additions & 0 deletions .changeset/sharp-dots-repeat.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@farmfe-examples/arco-design-pro": patch
---

fix(examples): examples/arco-pro useEffect does not clear side effects
15 changes: 12 additions & 3 deletions examples/arco-pro/src/pages/dashboard/workplace/announcement.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -12,19 +12,28 @@ function Announcement() {
const t = useLocale(locale)

const fetchData = () => {
let isMounted = true
setLoading(true)
axios
.get('/api/workplace/announcement')
.then((res) => {
setData(res.data)
if (isMounted) {
setData(res.data)
}
})
.finally(() => {
setLoading(false)
if (isMounted) {
setLoading(false)
}
})
return () => {
isMounted = false
};
}

useEffect(() => {
fetchData()
const cleanup = fetchData();
return cleanup;
}, [])

function getTagColor(type) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,19 +11,28 @@ function PopularContent() {
const [loading, setLoading] = useState(true)

const fetchData = () => {
let isMounted = true;
setLoading(true)
axios
.get('/api/workplace/content-percentage')
.then((res) => {
setData(res.data)
if (isMounted) {
setData(res.data)
}
})
.finally(() => {
setLoading(false)
if (isMounted) {
setLoading(false)
}
})
return () => {
isMounted = false;
};
}

useEffect(() => {
fetchData()
const cleanup = fetchData();
return cleanup;
}, [])

return (
Expand Down
17 changes: 13 additions & 4 deletions examples/arco-pro/src/pages/dashboard/workplace/overview.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -57,21 +57,30 @@ function Overview() {
const userInfo = useSelector((state: any) => state.userInfo || {})

const fetchData = () => {
let isMounted = true;
setLoading(true)
axios
.get('/api/workplace/overview-content')
.then((res) => {
setData(res.data)
if (isMounted) {
setData(res.data);
}
})
.finally(() => {
setLoading(false)
if (isMounted) {
setLoading(false);
}
})
return () => {
isMounted = false;
};
}

useEffect(() => {
fetchData()
const cleanup = fetchData();
return cleanup;
}, [])

return (
<Card>
<Typography.Title heading={5}>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,22 +15,31 @@ function PopularContent() {
const [total, setTotal] = useState(0)

const fetchData = useCallback(() => {
let isMounted = true;
setLoading(true)
axios
.get(
`/api/workplace/popular-contents?page=${page}&pageSize=5&category=${type}`,
)
.then((res) => {
setData(res.data.list)
setTotal(res.data.total)
if (isMounted) {
setData(res.data.list)
setTotal(res.data.total)
}
})
.finally(() => {
setLoading(false)
if (isMounted) {
setLoading(false)
}
})
return () => {
isMounted = false;
};
}, [page, type])

useEffect(() => {
fetchData()
const cleanup = fetchData();
return cleanup;
}, [page, fetchData])

const columns = [
Expand Down

0 comments on commit 673e844

Please sign in to comment.