Skip to content
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

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open

Conversation

maksym-kostetskyi
Copy link

Copy link

@mateacademy-ai-mentor mateacademy-ai-mentor 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 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
Comment on lines 22 to 25
componentWillUnmount(): void {
document.removeEventListener('keyup', (e: KeyboardEvent) =>
this.handleKeypress(e),
);

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.

Copy link

@mateacademy-ai-mentor mateacademy-ai-mentor left a 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:

  1. Event Listener Management: The handleKeypress method currently returns a new function each time it is called. This leads to different function references being used in addEventListener and removeEventListener, preventing the event listener from being correctly removed in componentWillUnmount. To resolve this, define handleKeypress as a class method without returning a function, and use it directly in both addEventListener and removeEventListener.

  2. 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
Comment on lines 12 to 13
handleKeypress() {
return (e: KeyboardEvent) => this.setState({ pressedKey: e.key });

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());

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());

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.

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.

2 participants