Skip to content
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

fix(examples): examples/arco-pro useEffect does not clear side effects #1283

Merged
merged 4 commits into from
May 6, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading