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

chore: enable rules of hooks for RN chat SDK #2626

Merged
merged 2 commits into from
Aug 26, 2024

Conversation

isekovanic
Copy link
Contributor

🎯 Goal

As @santhoshvai pointed out in this Slack thread, the Rules of Hooks eslint plugin has been disabled for the Chat SDK. We want to re-enable it so that future development follows these rules intrinsically.

🛠 Implementation details

This is mostly a port of GetStream/stream-chat-react#2244 (thanks @myandrienko !) to our SDK.

Brief rundown of the steps:

  • Enable the react-hooks/exhaustive-deps plugin
  • Run yarn eslint 'src/**/*.{js,ts,tsx,md}' -f json -o lint.json in all directories for which the plugin was enabled
  • Execute the script mentioned in the PR above (be mindful to change const json = require('./lint.json'); to whichever path your lint.json files got generated at)
  • Run yarn prettier --write to fix all of the formatting issues
  • Finally, run yarn eslint again to make sure that no warnings pop up after the script had been run

Care that prettier might also do some other changes in additional files - everything was checked by hand as well after the script was run and odd edge cases were handled adhoc (in my case they were only prettier ones).

🎨 UI Changes

iOS
Before After
Android
Before After

🧪 Testing

☑️ Checklist

  • I have signed the Stream CLA (required)
  • PR targets the develop branch
  • Documentation is updated
  • New code is tested in main example apps, including all possible scenarios
    • SampleApp iOS and Android
    • Expo iOS and Android

@Stream-SDK-Bot
Copy link
Contributor

SDK Size

title develop branch diff status
js_bundle_size 438.4580078125 KB 438 KB 0 B 🟢

@@ -25,11 +25,13 @@ export const useViewport = (rounded?: boolean) => {
return () => subscriptions?.remove();
}, []);

// eslint-disable-next-line react-hooks/exhaustive-deps
Copy link
Member

Choose a reason for hiding this comment

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

are these false positives? curious why the script detects these as hook deps :)

Copy link
Contributor Author

@isekovanic isekovanic Aug 22, 2024

Choose a reason for hiding this comment

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

Not too sure why the eslint-disable-next-line happens here either (instead of useMemo itself), but they're essentially here because useMemo is invoked every time and these aren't memoized at all (defined inline, will change on every render), while consuming resources as React memoization does. It's probably because if we want to actually memoize these, we should either:

  • Move the function definitions inside of the useMemo (and make it depend on viewportDimensions.height/viewportDimensions.width
  • Wrap the definitions within useCallback (with their respective dependencies of width/height), while getting rid of useMemo altogether

In my opinion these shouldn't be subject to memoization at all since I'm pretty sure the memoization itself is more expensive than directly setting them to some state (within the event listener) and just passing them like so. But we can do it incrementally and see what sticks.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

That is, assuming some downstream project doesn't actually rely on the fact that these aren't memoized :D that might be a bit tricky

Copy link
Member

Choose a reason for hiding this comment

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

I think it would be best if we remove useMemo(() => ({ vh, vw }), [vh, vw]); and return directly.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yep, after I'm done with some other things I'll jump into taking care of these easier low hanging fruits which should improve performance a bit.

Copy link
Member

Choose a reason for hiding this comment

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

nice catch here, this useMemo does nothing here, useCallback for both vw and vh and then remove useMemo here

I think vw and vh is used in multiple React.Memos

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yep, I'll have to look how far the non-memoization cascades here. Also I hope that some downstream implementations do not do not rely on the fact that vh and vw aren't memoized and that we break them. Should we add this somewhere ?

@isekovanic isekovanic merged commit b99833c into develop Aug 26, 2024
7 checks passed
@isekovanic isekovanic deleted the chore/enable-rules-of-hooks branch August 26, 2024 14:12
@stream-ci-bot
Copy link
Contributor

🎉 This PR is included in version 5.36.0 🎉

The release is available on:

Your semantic-release bot 📦🚀

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants