Skip to content

Commit a384a1b

Browse files
committed
Bug 1849880 - Save to Pocket fixing eslint dependencies warning around useEffect. r=gvn
Differential Revision: https://phabricator.services.mozilla.com/D186975
1 parent b16c6e1 commit a384a1b

File tree

3 files changed

+59
-53
lines changed

3 files changed

+59
-53
lines changed

browser/components/pocket/content/panels/css/main.compiled.css

Lines changed: 1 addition & 3 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

browser/components/pocket/content/panels/js/components/Home/Home.jsx

Lines changed: 30 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
* License, v. 2.0. If a copy of the MPL was not distributed with this file,
33
* You can obtain one at http://mozilla.org/MPL/2.0/. */
44

5-
import React, { useState, useEffect } from "react";
5+
import React, { useState, useEffect, useCallback } from "react";
66
import Header from "../Header/Header";
77
import ArticleList from "../ArticleList/ArticleList";
88
import PopularTopics from "../PopularTopics/PopularTopics";
@@ -19,6 +19,7 @@ function Home(props) {
1919
utmCampaign,
2020
utmContent,
2121
} = props;
22+
2223
const [{ articles, status }, setArticlesState] = useState({
2324
articles: [],
2425
// Can be success, loading, or error.
@@ -31,41 +32,47 @@ function Home(props) {
3132
: ``
3233
}`;
3334

35+
const loadingRecentSaves = useCallback(resp => {
36+
setArticlesState(prevState => ({
37+
...prevState,
38+
status: "loading",
39+
}));
40+
}, []);
41+
42+
const renderRecentSaves = useCallback(resp => {
43+
const { data } = resp;
44+
45+
if (data.status === "error") {
46+
setArticlesState(prevState => ({
47+
...prevState,
48+
status: "error",
49+
}));
50+
return;
51+
}
52+
53+
setArticlesState({
54+
articles: data,
55+
status: "success",
56+
});
57+
}, []);
58+
3459
useEffect(() => {
3560
if (!hideRecentSaves) {
3661
// We don't display the loading message until instructed. This is because cache
3762
// loads should be fast, so using the loading message for cache just adds loading jank.
3863
panelMessaging.addMessageListener(
3964
"PKT_loadingRecentSaves",
40-
function (resp) {
41-
setArticlesState({
42-
articles,
43-
status: "loading",
44-
});
45-
}
65+
loadingRecentSaves
4666
);
4767

4868
panelMessaging.addMessageListener(
4969
"PKT_renderRecentSaves",
50-
function (resp) {
51-
const { data } = resp;
52-
53-
if (data.status === "error") {
54-
setArticlesState({
55-
articles: [],
56-
status: "error",
57-
});
58-
return;
59-
}
60-
61-
setArticlesState({
62-
articles: data,
63-
status: "success",
64-
});
65-
}
70+
renderRecentSaves
6671
);
6772
}
73+
}, [hideRecentSaves, loadingRecentSaves, renderRecentSaves]);
6874

75+
useEffect(() => {
6976
// tell back end we're ready
7077
panelMessaging.sendMessage("PKT_show_home");
7178
}, []);

browser/components/pocket/content/panels/js/main.bundle.js

Lines changed: 28 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -290,37 +290,38 @@ function Home(props) {
290290
status: ""
291291
});
292292
const utmParams = `utm_source=${utmSource}${utmCampaign && utmContent ? `&utm_campaign=${utmCampaign}&utm_content=${utmContent}` : ``}`;
293+
const loadingRecentSaves = (0,react.useCallback)(resp => {
294+
setArticlesState(prevState => ({ ...prevState,
295+
status: "loading"
296+
}));
297+
}, []);
298+
const renderRecentSaves = (0,react.useCallback)(resp => {
299+
const {
300+
data
301+
} = resp;
302+
303+
if (data.status === "error") {
304+
setArticlesState(prevState => ({ ...prevState,
305+
status: "error"
306+
}));
307+
return;
308+
}
309+
310+
setArticlesState({
311+
articles: data,
312+
status: "success"
313+
});
314+
}, []);
293315
(0,react.useEffect)(() => {
294316
if (!hideRecentSaves) {
295317
// We don't display the loading message until instructed. This is because cache
296318
// loads should be fast, so using the loading message for cache just adds loading jank.
297-
messages.addMessageListener("PKT_loadingRecentSaves", function (resp) {
298-
setArticlesState({
299-
articles,
300-
status: "loading"
301-
});
302-
});
303-
messages.addMessageListener("PKT_renderRecentSaves", function (resp) {
304-
const {
305-
data
306-
} = resp;
307-
308-
if (data.status === "error") {
309-
setArticlesState({
310-
articles: [],
311-
status: "error"
312-
});
313-
return;
314-
}
315-
316-
setArticlesState({
317-
articles: data,
318-
status: "success"
319-
});
320-
});
321-
} // tell back end we're ready
322-
323-
319+
messages.addMessageListener("PKT_loadingRecentSaves", loadingRecentSaves);
320+
messages.addMessageListener("PKT_renderRecentSaves", renderRecentSaves);
321+
}
322+
}, [hideRecentSaves, loadingRecentSaves, renderRecentSaves]);
323+
(0,react.useEffect)(() => {
324+
// tell back end we're ready
324325
messages.sendMessage("PKT_show_home");
325326
}, []);
326327
let recentSavesSection = null;

0 commit comments

Comments
 (0)