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

Autobind replacement and react-hot-reload update #3022

Merged
merged 9 commits into from Feb 2, 2021
Merged

Autobind replacement and react-hot-reload update #3022

merged 9 commits into from Feb 2, 2021

Conversation

MrSnix
Copy link
Contributor

@MrSnix MrSnix commented Jan 22, 2021

As requested by @develohpanda, I'm opening this PR as a proposal about issue #2468.
This PR is an improved version from the previous one #2963 which has been closed in favor of this one.

Context
The auto-bind decorator is preventing react-hot-reload from working properly due to binding not only the class functions but even the React lifecycles functions (which is unnecessary and should be avoided) causing the following error:
image

The current idea
We aim to replace autobind-decorator with class-autobind-decorator that introduces @autoBindMethodsForReact a convenience decorator that skips auto binding methods from the React Component Spec that do not require binding.

Here is a gif that shows react-hot-reloading working again

Test

As always I'm glad to hear feedback.
Thanks

Closes #2468

@netlify
Copy link

netlify bot commented Jan 22, 2021

Deploy preview for insomnia-storybook ready!

Built with commit 41e2a8c

https://deploy-preview-3022--insomnia-storybook.netlify.app

Copy link
Contributor

@develohpanda develohpanda left a comment

Choose a reason for hiding this comment

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

Much cleaner PR, and it seems to be working well too. Nice! 🎉

react-fast-refresh isn't stable for Webpack yet, but that would be the next transition when the time comes.

Copy link
Contributor

@DMarby DMarby left a comment

Choose a reason for hiding this comment

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

Thank you for doing this, seems to work great overall!

I noticed that it seems to break some functionality in the sidebar upon hot reloading, but I'll defer to @develohpanda as to whether that's best to fix before merging this or if it can be addressed later.

I'm not sure if we need to also manually ignore some of the lifecycle methods in the files where we use them @develohpanda?
The library automatically ignores these ones: https://github.com/jmrog/class-autobind-decorator/blob/master/src/lib/ReactComponentSpecMethods.js
However, some of them that we use have changed names, and might need to be manually ignored:

UNSAFE_componentWillMount
UNSAFE_componentWillUpdate
UNSAFE_componentWillReceiveProps

Not sure if there are also any new lifecycle methods we use that should be ignored.
Regardless, that could probably be addressed at a future date.

packages/insomnia-app/app/ui/index.js Outdated Show resolved Hide resolved
@MrSnix
Copy link
Contributor Author

MrSnix commented Jan 28, 2021

In the last commit, I manually added unsafe react lifecycles as to be ignored by the decorator.
About the sidebar error, as @DMarby correctly stated, it is related to react-dnd and react-hot-reload. (We are so lucky...)
I found this: react-dnd #186
I'll try to ship a fix for this if I manage to.

EDIT:
I fixed it. Issue no more... I'll play it a bit to see if it works as expected and I'll push the commit.
@develohpanda If you will check it... because I'm not really familiar with react-dnd.

Copy link
Contributor

@develohpanda develohpanda left a comment

Choose a reason for hiding this comment

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

Yes the UNSAFE_* lifecycle methods should be ignored too (and aren't currently as a result of the library being slightly outdated).

Nice work adding them @MrSnix 🎉, although (if it works) I think it might be safer to apply a common autoBindConfig to each instance of the decorator, rather than selectively depending on usage in a component. If specified per component, as it changes over time it is likely someone will forget to update the autoBind config for it.

// common.js or somewhere
const autoBindConfig = {
  methodsToIgnore: ['UNSAFE_componentWillReceiveProps', ...],
};

// component file
@autoBindMethodsForReact(autoBindConfig)
class Component { ... }

Copy link
Contributor

@develohpanda develohpanda left a comment

Choose a reason for hiding this comment

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

🔥

@develohpanda develohpanda merged commit c4babfc into Kong:develop Feb 2, 2021
@MrSnix MrSnix deleted the autobind-fix branch February 3, 2021 05:14
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.

Remove usages of @autobind to allow for react-hot-reloading to work
3 participants