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

refactor(searchbox): Remove unnecessary useEffect hook #6718

Open
wants to merge 2 commits into
base: main
Choose a base branch
from

Conversation

rahulkhattri0
Copy link

@rahulkhattri0 rahulkhattri0 commented May 11, 2024

Description

image

Removes this unnecessary useEffect in the withSearchBox component.This useEffect can be removed because we have only 2 event handlers where this search function should be called(which is not a lot).

Also,
image
This effect properly calls the search api with '' as the search term and sets the initial search results so we dont need the above effect(the one that I removed) to run on mount with the empty search term and for subsequent changes in the search term and facet we can call the search function in the event handlers as explained above.

Related Issues

Please refer #6717 to know more.

Check List

  • I have read the Contributing Guidelines and made commit messages that follow the guideline.
  • I have run npm run format to ensure the code follows the style guide.
  • I have run npm run test to check if all tests are passing.
  • I have run npx turbo build to check if the website builds without errors.
  • I've covered new added functionality with unit tests if necessary.

@rahulkhattri0 rahulkhattri0 requested a review from a team as a code owner May 11, 2024 08:29
Copy link

vercel bot commented May 11, 2024

The latest updates on your projects. Learn more about Vercel for Git ↗︎

Name Status Preview Updated (UTC)
nodejs-org ✅ Ready (Inspect) Visit Preview May 11, 2024 10:18am

Copy link

github-actions bot commented May 11, 2024

Lighthouse Results

URL Performance Accessibility Best Practices SEO Report
/en 🟠 89 🟢 100 🟢 100 🟢 91 🔗
/en/about 🟢 99 🟢 100 🟢 100 🟢 91 🔗
/en/about/previous-releases 🟢 99 🟢 100 🟢 100 🟢 92 🔗
/en/download 🟢 100 🟢 100 🟢 100 🟢 91 🔗
/en/blog 🟢 100 🟢 100 🟢 96 🟢 92 🔗

Copy link

Unit Test Coverage Report

Lines Statements Branches Functions
Coverage: 91%
90.04% (588/653) 76.08% (175/230) 92.18% (118/128)

Unit Test Report

Tests Skipped Failures Errors Time
128 0 💤 0 ❌ 0 🔥 5.931s ⏱️

Copy link
Member

@ovflowd ovflowd left a comment

Choose a reason for hiding this comment

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

This PR changes way more than just removing a useEffect, could you explain the reasoning behind these changes?

@rahulkhattri0
Copy link
Author

I have added the search function call in the event handlers as explained and I had to introduce the current selected facet parameter in the function.The function already had search term as an argument and I had to add the current selected facet because if I didn't, then the function would operate on a stale value of the selected facet.

@ovflowd
Copy link
Member

ovflowd commented May 12, 2024

I have added the search function call in the event handlers as explained and I had to introduce the current selected facet parameter in the function.The function already had search term as an argument and I had to add the current selected facet because if I didn't, then the function would operate on a stale value of the selected facet.

Interesting, I assume with an effect you didn't need to do so as it would already trigger a re-render, or?

@rahulkhattri0
Copy link
Author

rahulkhattri0 commented May 12, 2024

See using the effect is fine,but the way the effect is used right now especially with the use of // eslint-disable-next-line react-hooks/exhaustive-deps is not really a good practice.Also,useEffect must be avoided where it can be and here since we do not have a lot of event handlers to manage,useEffect can be avoided.

@ovflowd
Copy link
Member

ovflowd commented May 13, 2024

See using the effect is fine,but the way the effect is used right now especially with the use of // eslint-disable-next-line react-hooks/exhaustive-deps is not really a good practice.Also,useEffect must be avoided where it can be and here since we do not have a lot of event handlers to manage,useEffect can be avoided.

Disabling an ESLint rule inline isn't a bad practice. The rule of hooks is "automatic" based on dependencies you're using, but it doesn't mean you need to pass those dependencies as triggers for the effect to re-run

@@ -135,14 +126,17 @@ export const WithSearchBox: FC<SearchBoxProps> = ({ onClose }) => {
router.push(`/search?q=${searchTerm}&section=${selectedFacetName}`);
};

const changeFacet = (idx: string) => setSelectedFacet(Number(idx));
const changeFacet = (idx: string) => {
Copy link
Member

Choose a reason for hiding this comment

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

Instead of casting the "idx" twice as a Number, why not cast it within the calls of changeFacet?

Copy link
Member

Choose a reason for hiding this comment

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

It would, as it would explicitly tell that a facet must be a number. Otherwise I can pass "blah blah" as a param a a number conversion would be NaN; It enforces that whoever is calling the method is responsible for guaranteeing a number.

Copy link
Author

Choose a reason for hiding this comment

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

onValueChange={changeFacet} changeFacet is being passed in the Tabs component.Its type must be (value: string) => void.

@rahulkhattri0
Copy link
Author

rahulkhattri0 commented May 13, 2024

See using the effect is fine,but the way the effect is used right now especially with the use of // eslint-disable-next-line react-hooks/exhaustive-deps is not really a good practice.Also,useEffect must be avoided where it can be and here since we do not have a lot of event handlers to manage,useEffect can be avoided.

Disabling an ESLint rule inline isn't a bad practice. The rule of hooks is "automatic" based on dependencies you're using, but it doesn't mean you need to pass those dependencies as triggers for the effect to re-run

You can have a look at https://github.com/facebook/create-react-app/issues/6880

@ovflowd
Copy link
Member

ovflowd commented May 13, 2024

See using the effect is fine,but the way the effect is used right now especially with the use of // eslint-disable-next-line react-hooks/exhaustive-deps is not really a good practice.Also,useEffect must be avoided where it can be and here since we do not have a lot of event handlers to manage,useEffect can be avoided.

Disabling an ESLint rule inline isn't a bad practice. The rule of hooks is "automatic" based on dependencies you're using, but it doesn't mean you need to pass those dependencies as triggers for the effect to re-run

You can have a look at https://github.com/facebook/create-react-app/issues/6880

I'm not here to entertain you or discuss React's best practices. I won't enter on an argument with you on WHY what you're claiming is nonsensical. Simply linking to a random issue that has nothing to do with our conversation is baseless. The issue you've linked talks about "disabling" the eslint rule, which is indeed bad practice.

What I'm referring is that it is a common practice to disable the rule at times when you believe it is misreporting, the eslint rule is known for always marking all the dependencies you're referring to be part as explicit dependencies for the hook lifecycle. But in many cases, you don't want to include all dependencies, nor need, intentionally. As changing the dependencies changes the behaviour of the Hook itself.

Copy link
Member

@ovflowd ovflowd left a comment

Choose a reason for hiding this comment

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

It is clear to me after a few exchange of messages that you don't know what you're doing; This change also is purely cosmetical as I can't really see any bug or behaviour difference between the current Production Environment and this PR;

In general PRs that change code just because of "stylistic" reasons or that don't actually fix a bug are not needed. I'm keen to be proven wrong and that actually this change improves the performance/behaviour of the search box.

For the meantime, I'm against the change.

@rahulkhattri0
Copy link
Author

It is clear to me after a few exchange of messages that you don't know what you're doing; This change also is purely cosmetical as I can't really see any bug or behaviour difference between the current Production Environment and this PR;

In general PRs that change code just because of "stylistic" reasons or that don't actually fix a bug are not needed. I'm keen to be proven wrong and that actually this change improves the performance/behaviour of the search box.

For the meantime, I'm against the change.

I certainly know what I am doing and I was just trying to improve the code quality a bit.

@ovflowd
Copy link
Member

ovflowd commented May 13, 2024

It is clear to me after a few exchange of messages that you don't know what you're doing; This change also is purely cosmetical as I can't really see any bug or behaviour difference between the current Production Environment and this PR;
In general PRs that change code just because of "stylistic" reasons or that don't actually fix a bug are not needed. I'm keen to be proven wrong and that actually this change improves the performance/behaviour of the search box.
For the meantime, I'm against the change.

I certainly know what I am doing and I was just trying to improve the code quality a bit.

I understand you have good motivations, But this change, in my eyes, isn't a code quality improvement. It feels like a very subjective change, and if you have claimed that these changes actually "improve" the code, it'd be interesting for you to:

  • Explain what exactly is being improved and why current behaviour is not good, and how the current behaviour is impacting the code negatively
  • Benchmarks/References or Screenshots/GIFs that prove visual changes of such benefits

In my understanding, there isn't a real improvement here. That doesn't mean I'm right, and other folks might disagree with me. But for the meantime I just don't see clear benefits with this PR.

I understand that this doesn't help you, but we, as a project, can't just accept every little PR that claims to fix or improve something.

@rahulkhattri0
Copy link
Author

It is clear to me after a few exchange of messages that you don't know what you're doing; This change also is purely cosmetical as I can't really see any bug or behaviour difference between the current Production Environment and this PR;
In general PRs that change code just because of "stylistic" reasons or that don't actually fix a bug are not needed. I'm keen to be proven wrong and that actually this change improves the performance/behaviour of the search box.
For the meantime, I'm against the change.

I certainly know what I am doing and I was just trying to improve the code quality a bit.

I understand you have good motivations, But this change, in my eyes, isn't a code quality improvement. It feels like a very subjective change, and if you have claimed that these changes actually "improve" the code, it'd be interesting for you to:

  • Explain what exactly is being improved and why current behaviour is not good, and how the current behaviour is impacting the code negatively
  • Benchmarks/References or Screenshots/GIFs that prove visual changes of such benefits

In my understanding, there isn't a real improvement here. That doesn't mean I'm right, and other folks might disagree with me. But for the meantime I just don't see clear benefits with this PR.

I understand that this doesn't help you, but we, as a project, can't just accept every little PR that claims to fix or improve something.

Yeah ok I see your point.

@ovflowd
Copy link
Member

ovflowd commented May 13, 2024

Let's keep this PR open and wait for other reviewers to weigh in. I also apologize for the harsh tone in one of my previous messages; it was uncalled for.

@rahulkhattri0
Copy link
Author

Let's keep this PR open and wait for other reviewers to weigh in. I also apologize for the harsh tone in one of my previous messages; it was uncalled for.

No worries for that.

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.

None yet

2 participants