-
Notifications
You must be signed in to change notification settings - Fork 26
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
Refactor: wallet
screen to functional component
#488
Conversation
Codecov ReportAttention:
Additional details and impacted files@@ Coverage Diff @@
## dev #488 +/- ##
========================================
- Coverage 9.35% 9.21% -0.15%
========================================
Files 112 112
Lines 5161 5242 +81
Branches 698 696 -2
========================================
Hits 483 483
- Misses 4015 4098 +83
+ Partials 663 661 -2 ☔ View full report in Codecov by Sentry. |
878f69e
to
bb5e3ad
Compare
src/screens/Wallet.js
Outdated
const [backupDone, setBackupDone] = useState(LOCAL_STORE.isBackupDone()); | ||
/** successMessage {string} Message to be shown on alert success */ | ||
const [successMessage, setSuccessMessage] = useState(''); | ||
/* shouldShowAdministrativeTab {boolean} If we should display the Administrative Tools tab */ | ||
const [shouldShowAdministrativeTab, setShouldShowAdministrativeTab] = useState(false); | ||
const [errorMessage, setErrorMessage] = useState(''); // TODO: Metadata token error are being suppressed as of now | ||
const [totalSupply, setTotalSupply] = useState(null); | ||
const [canMint, setCanMint] = useState(false); | ||
const [canMelt, setCanMelt] = useState(false); | ||
const [transactionsCount, setTransactionsCount] = useState(null); | ||
const [mintCount, setMintCount] = useState(null); | ||
const [meltCount, setMeltCount] = useState(null); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
[suggestion] Why not aggregate all the states in an object and use a single useState
?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I initially implemented it like that because it's the most common pattern for functional components: a getter/setter for each property.
Upon further investigation, I've seen that by having a state for each property we allow React to optimize when to re-render each element in the screen. If we had a single state object, whenever we updated any of its properties the whole screen would be re-rendered.
In practice, this seems to have almost no performance impact, so it's left as a style decision. I even thought about having walletInfo: { mintCount, meltCount }
and tokenInfo: { canMint, canMelt, txCount }
, but it didn't feel like it would improve the legibility of the code.
What do you think?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don't have much to say. If it is better to use this way lets keep it.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It's usually preferable to use each property as a different state so we can pass them as dependencies on useCallback
and useEffect
and other hooks, allowing react to memoize those hooks
src/screens/Wallet.js
Outdated
import hathorLib from '@hathor/wallet-lib'; | ||
import { t } from 'ttag'; | ||
import { get } from 'lodash'; | ||
import { connect } from "react-redux"; | ||
import { useDispatch, useSelector } from "react-redux"; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
import { useDispatch, useSelector } from "react-redux"; | |
import { useDispatch, useSelector } from 'react-redux'; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Fixed on f85bf7c
src/screens/Wallet.js
Outdated
useWalletService: state.useWalletService, | ||
}; | ||
}; | ||
import { useHistory } from "react-router-dom"; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is the linter passing?
import { useHistory } from "react-router-dom"; | |
import { useHistory } from 'react-router-dom'; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
src/screens/Wallet.js
Outdated
const [successMessage, setSuccessMessage] = useState(''); | ||
/* shouldShowAdministrativeTab {boolean} If we should display the Administrative Tools tab */ | ||
const [shouldShowAdministrativeTab, setShouldShowAdministrativeTab] = useState(false); | ||
const [errorMessage, setErrorMessage] = useState(''); // TODO: Metadata token error are being suppressed as of now |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
So why should we leave this here?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Even though issue #487 is already open to fix this I found it best to leave it as a warning in the code itself, because this is a bug.
Do you agree with leaving it as a comment?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I mean, setErrorMessage
is not being used anywhere in this file, and it's a generic error message
So why keep it there?
Maybe remove the const [errorMessage, setErrorMessage] = useState('');
part and update the comment to explain that metadata fetch errors are being ignored
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done on 8098579
628700f
to
8098579
Compare
Acceptance Criteria
wallet
screen should be refactored to a functional componentSecurity Checklist