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

no-use-state-initializer-functions rule #138

Merged

Conversation

rezkiy37
Copy link
Contributor

@rezkiy37 rezkiy37 commented Nov 28, 2024

This PR implements a new ESLint rule to detect and restricts usage initializer functions for useState in E/App. Basically, when a function is being called directly within the useState hook, which means it gets executed on every render, even though it only needs to run during the initial state setup.

const [state, setState] = useState(initStateFunc());

We can pass a callback to the useState hook instead. This ensures the function is only executed once during the initial render, improving performance:

const [state, setState] = useState(() => initStateFunc());

By making this change, e.g. BottomTabBar avoids unnecessary re-execution of getChatTabBrickRoad on every render. This optimization reduces the render time from approximately ±500ms to just 50-100ms, resulting in a more than 5x performance improvement (Expensify/App#53201). This ESLint rule detects these situations to avoid such perf. problems like Expensify/App#53162.

Issue: Expensify/App#53322, Expensify/App#53162

Tests

  1. Checkout this branch.
  2. Go to E/App repo and run this command to link your eslint-config-expensify repo to E/App: npx link publish <path-to-eslint-repo>/.
  3. Make change in a file that has plenty of examples e.g. src/components/TimePicker/TimePicker.tsx, and commit that file.
+ const [hours, setHours] = useState(DateUtils.get12HourTimeObjectFromDate(value, showFullFormat).hour);
+ const [minutes, setMinutes] = useState(DateUtils.get12HourTimeObjectFromDate(value, showFullFormat).minute);
+ const [seconds, setSeconds] = useState(DateUtils.get12HourTimeObjectFromDate(value, showFullFormat).seconds);
+ const [milliseconds, setMilliseconds] = useState(DateUtils.get12HourTimeObjectFromDate(value, showFullFormat).milliseconds);
+ const [amPmValue, setAmPmValue] = useState(DateUtils.get12HourTimeObjectFromDate(value, showFullFormat).period);
  1. Run npm run lint-changed, assert the errors were reported.
Screenshots Screenshot 2024-11-29 at 16 23 21 Screenshot 2024-11-29 at 16 23 52

Copy link
Contributor

github-actions bot commented Nov 28, 2024

All contributors have signed the CLA ✍️ ✅
Posted by the CLA Assistant Lite bot.

@rezkiy37
Copy link
Contributor Author

I have read the CLA Document and I hereby sign the CLA

@rezkiy37
Copy link
Contributor Author

rezkiy37 commented Dec 2, 2024

@mountiny, this PR is ready for review 🙂

@mountiny
Copy link
Contributor

mountiny commented Dec 2, 2024

@alitoshmatov Can you please test this PR and review it? thanks!

@alitoshmatov
Copy link

@rezkiy37 I think we should also cover a case where useState(condition?a():b()). Based on my testing this is not handled

@rezkiy37
Copy link
Contributor Author

rezkiy37 commented Dec 3, 2024

@alitoshmatov, done - fa4c5fd.

Copy link

@alitoshmatov alitoshmatov left a comment

Choose a reason for hiding this comment

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

LGTM!

Screenshots

Screenshot 2024-12-02 at 11 43 37 PM Screenshot 2024-12-02 at 11 45 31 PM Screenshot 2024-12-02 at 11 45 39 PM Screenshot 2024-12-02 at 11 52 58 PM Screenshot 2024-12-02 at 11 53 10 PM

Copy link
Contributor

@mountiny mountiny left a comment

Choose a reason for hiding this comment

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

Thanks! lets get this in App now 🚀

@rezkiy37 rezkiy37 force-pushed the feature/no-use-state-initializer-functions branch from fb1f439 to 9420237 Compare December 4, 2024 12:05
@rezkiy37
Copy link
Contributor Author

rezkiy37 commented Dec 4, 2024

I've pushed commits forcefully to sign them.

cc @alitoshmatov @mountiny

@mountiny mountiny merged commit eef61d2 into Expensify:main Dec 4, 2024
3 checks passed
@os-botify
Copy link
Contributor

os-botify bot commented Dec 4, 2024

🚀 Published to npm in 2.0.74 🎉

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.

3 participants