Skip to content

Conversation

@theSaLi
Copy link
Collaborator

@theSaLi theSaLi commented May 30, 2021

Hi, @changdiqing @Apollo1840,

I fixed the problem with useEffect and localStorage.
useEffect updates the m (minutes) and s (seconds) and saves them to localStorage.
After refreshing or switching the view, m and s are read from the localStorage and set to be the initialRemainingTime of the timer.

Please check the PR. Thanks.

@theSaLi theSaLi requested review from Apollo1840 and changdiqing May 30, 2021 11:50
const [isClockPlaying, setIsClockPlaying] = useState(false);
const [workDurationMinutes, setWorkDurationMinutes] = useState(25);
const [breakDurationMinutes, setBreakDurationMinutes] = useState(5);
const [m, setM] = useState(25);
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[minute, setMinute] = useState(25):
[second, setSecond] = useState(0);

in this way, it could be more obvious for anyone who read m anywhere to know it stands for minutes.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

good point. corrected.

localStorage.setItem("S", s)
})

function setData(func, key) {
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

setData sounds not so direct for me. and it could be more in consistent with localStorage.setItem by

Suggested change
function setData(func, key) {
function localStorage_loadItem(itemName, itemSetter, itemType) {}

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I used the name loadItemFromLocalStorage.

useEffect ( () => {
setData((data) => setStartTime(data), "startTime")
setData((data) => setIsUserBreaking(data == "true"), "isUserBreaking")
setData((data) => setClockKey(Number(data)), "ClockKey")
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I suggest to handle the data type (Number(), == "true" ) inside setData function via kwargs(function parameters).
so that the code could be easier to understand here

Suggested change
setData((data) => setClockKey(Number(data)), "ClockKey")
setData(setIsUserBreaking, "isUserBreaking", "bool")
setData(setClockKey, "ClockKey", "number")

How do you think?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

good point, corrected.

Copy link
Owner

@Apollo1840 Apollo1840 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It looks clear and effective, No more feedbacks from me. @changdiqing . Another option is to use redux store to do this, but I think we could leverage the pro and con a bit, and then make another PR, if the redux store is a better option.

loadItemFromLocalStorage("breakDurationMinutes", setBreakDurationMinutes, "number")
loadItemFromLocalStorage("Minute", setMinute, "number")
loadItemFromLocalStorage("Second", setSecond, "number")
loadItemFromLocalStorage("Second", setSecond, "number")
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Duplication

Copy link
Collaborator Author

@theSaLi theSaLi Jun 6, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks. Fixed.
One con of local storage is that it can only store string. The item has to be converted to its original type while loading. Another option to avoid the conversion I did is to stringify and parse the item during storing and loading.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants