-
Notifications
You must be signed in to change notification settings - Fork 3k
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
[NoQA] Improved initial state setting by calling initial setter only once #29630
[NoQA] Improved initial state setting by calling initial setter only once #29630
Conversation
Hey! I see that you made changes to our Form component. Make sure to update the docs in FORMS.md accordingly. Cheers! |
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.
LGTM!
@@ -20,7 +20,7 @@ const propTypes = { | |||
function NavigationAwareCamera({cameraTabIndex, forwardedRef, isInTabNavigator, ...props}) { | |||
// Get navigation to get initial isFocused value (only needed once during init!) | |||
const navigation = useNavigation(); | |||
const [isCameraActive, setIsCameraActive] = useState(navigation.isFocused()); | |||
const [isCameraActive, setIsCameraActive] = useState(navigation.isFocused); |
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: It looks like you're assigning the isFocused
method directly to useState
. Be cautious when doing this, as the method might depend on the context of this
from the navigation
object. To preserve the context, consider using a function within useState
like this:
const [isCameraActive, setIsCameraActive] = useState(navigation.isFocused); | |
const [isCameraActive, setIsCameraActive] = useState(() => navigation.isFocused()); |
This way, you ensure that isFocused
is called in the context it was defined in, avoiding any potential issues with losing this
context.
PS: Isn't ESLint flagging this behavior with a warning? I've noticed warnings about potentially losing the context of this
in similar scenarios. It could be specific to my editor setup (I'm using WebStorm), but it might be worth double-checking.
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'm aware that sometimes we can lose this
context, but I was not sure if that happens here (I've planned to test that). But I agree that it's better to be safe in this case, so I will change it to arrow function.
I don't recall seeing ESlint warning for such case in my setup (VScode)
const [selection, setSelection] = useState(getInitialSelection()); | ||
const [selection, setSelection] = useState(getInitialSelection); |
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.
👍 Kudos: Your change optimizes performance by preventing unnecessary parsing during each render cycle. This could result in a measurable improvement.
b391fe3
to
78cbb04
Compare
78cbb04
to
42d4127
Compare
@0xmiroslav Please copy/paste the Reviewer Checklist from here into a new comment on this PR and complete it. If you have the K2 extension, you can simply click: [this button] |
Reviewer Checklist
Screenshots/VideosMobile Web - ChromeMobile Web - SafariDesktopiOSAndroid |
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.
Seems to be working well, thank you!
✋ This PR was not deployed to staging yet because QA is ongoing. It will be automatically deployed to staging after the next production release. |
🚀 Deployed to staging by https://github.com/mountiny in version: 1.3.91-0 🚀
|
🚀 Deployed to production by https://github.com/marcaaron in version: 1.3.91-8 🚀
|
Details
Optimized setting initial state in functional components
Problem:
The code we put in initial state is invoked on each render, even if only first invocation is used for setting initial state.
Explained in React docs: https://react.dev/reference/react/useState#avoiding-recreating-the-initial-state
Fixed Issues
$ #30209
Tests
Offline tests
QA Steps
PR Author Checklist
### Fixed Issues
section aboveTests
sectionOffline steps
sectionQA steps
sectiontoggleReport
and notonIconClick
)myBool && <MyComponent />
.src/languages/*
files and using the translation methodWaiting for Copy
label for a copy review on the original GH to get the correct copy.STYLE.md
) were followedAvatar
, I verified the components usingAvatar
are working as expected)/** comment above it */
this
properly so there are no scoping issues (i.e. foronClick={this.submit}
the methodthis.submit
should be bound tothis
in the constructor)this
are necessary to be bound (i.e. avoidthis.submit = this.submit.bind(this);
ifthis.submit
is never passed to a component event handler likeonClick
)StyleUtils.getBackgroundAndBorderStyle(themeColors.componentBG)
)Avatar
is modified, I verified thatAvatar
is working as expected in all cases)ScrollView
component to make it scrollable when more elements are added to the page.main
branch was merged into this PR after a review, I tested again and verified the outcome was still expected according to theTest
steps.Screenshots/Videos
Android: Native
and-app-3.mov
Android: mWeb Chrome
and-brow-3_H.264.mp4
iOS: Native
ios-app-3.mov
iOS: mWeb Safari
ios-brow-3.mov
MacOS: Chrome / Safari
web-3.mov
MacOS: Desktop
desk-3.mov