-
Notifications
You must be signed in to change notification settings - Fork 1.9k
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
solution #1982
base: master
Are you sure you want to change the base?
solution #1982
Conversation
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 looks like there's a critical issue with the event listener in your App
component. The arrow function used in addEventListener
creates a new function reference, which prevents the event listener from being correctly removed in componentWillUnmount
. This can lead to memory leaks. Please define the event handler as a class method and use it consistently in both addEventListener
and removeEventListener
. Once you address this, your code will be in great shape! Keep up the good work! 😊
✨ Please rate this review in the Platform's notification center. I will be grateful for your feedback! ✨
🔄 Fix the issues, commit the changes, and then re-request my review using the Re-request review
button in the Reviewers tab.
src/App.tsx
Outdated
componentWillUnmount(): void { | ||
document.removeEventListener('keyup', (e: KeyboardEvent) => | ||
this.handleKeypress(e), | ||
); |
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.
The event listener is not being removed correctly in componentWillUnmount
. The arrow function used in addEventListener
creates a new function reference, which is different from the one used in removeEventListener
. To fix this, you should define the event handler as a class method and use it in both addEventListener
and removeEventListener
.
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.
Great job on your submission! 🎉 While your code is on the right track, there are a few critical issues that need attention to ensure robust functionality:
-
Event Listener Management: The
handleKeypress
method currently returns a new function each time it is called. This leads to different function references being used inaddEventListener
andremoveEventListener
, preventing the event listener from being correctly removed incomponentWillUnmount
. To resolve this, definehandleKeypress
as a class method without returning a function, and use it directly in bothaddEventListener
andremoveEventListener
. -
Code Style: Your method naming and ternary operator formatting adhere to the guidelines, which is great! Keep up the good work on maintaining clean and readable code.
Despite these issues, the decision to approve your pull request is made to help you move forward with your learning. Please review the comments and address the critical issue with the event listener to improve your code. This will not only enhance the functionality but also prevent potential memory leaks in the future. Keep learning and refining your skills! 🚀
✨ Please rate this review in the Platform's notification center. I will be grateful for your feedback! ✨
src/App.tsx
Outdated
handleKeypress() { | ||
return (e: KeyboardEvent) => this.setState({ pressedKey: e.key }); |
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.
The handleKeypress
method currently returns a new function each time it is called. This results in different function references being used in addEventListener
and removeEventListener
, which prevents the event listener from being correctly removed. Consider defining handleKeypress
as a class method without returning a function, and use it directly in both addEventListener
and removeEventListener
.
src/App.tsx
Outdated
} | ||
|
||
componentDidMount(): void { | ||
document.addEventListener('keyup', this.handleKeypress()); |
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.
Using this.handleKeypress()
here calls the method and returns a new function, which is not the same reference used in removeEventListener
. Define handleKeypress
as a class method and use this.handleKeypress
directly.
src/App.tsx
Outdated
} | ||
|
||
componentWillUnmount(): void { | ||
document.removeEventListener('keyup', this.handleKeypress()); |
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.
Using this.handleKeypress()
here calls the method and returns a new function, which is not the same reference used in addEventListener
. Define handleKeypress
as a class method and use this.handleKeypress
directly.
DEMO LINK