-
Notifications
You must be signed in to change notification settings - Fork 47.3k
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
useCallback()
& useMemo()
automatically with a Babel Plug-in
#14658
Comments
I’m sorry but I think you misread the content of the issue #14406. No one is talking about runtime checks. Indeed I created |
@yuchi Your'e right I misread it. However, why do we need to use hooks like |
@DAB0mB super cool! I was thinking it would be even better if it could wrap the |
@jedwards1211 Everything's possible. Could you give me a sample input/output because I'm not sure exactly what do you mean. Also try to look at @yuchi's implementation, maybe you'll find something interesting. |
@DAB0mB yeah, actually I put some examples at DAB0mB/babel-plugin-react-persist#1 |
Maybe at some point I could contribute a PR myself |
Ok, it took some time before I could look deeply at this proposal. The approach is very intersting IMHO, your issue description doesn’t explain all the optimiziations introduced. So, let me share some ideas:
|
@yuchi I had a comment on another issue about how in my mind the ideal, pie-in-the-sky React compiler would be able to completely compile away virtual DOM diffing where possible...:smile: Can't find that issue at the moment |
@yuchi I'm just expecting to have a solution that works right out of the box. I don't even wanna think about it. If a machine can do it better than me, why should I even bother? If there would be enough awareness for this plug-in and activity from the community, there would be almost non to no bugs. Of course there are bugs in what I created, but it's only a proof of concept. About using |
Do you want to request a feature or report a bug?
Feature request.
What is the current behavior?
We need to
useCallback()
anduseMemo()
which seems redundant and can cost us in performance if not used right, which is likely to happen.If the current behavior is a bug, please provide the steps to reproduce and if possible a minimal demo of the problem. Your bug will get fixed much faster if we can run your code and it doesn't have dependencies other than React. Paste the link to your JSFiddle (https://jsfiddle.net/Luktwrdm/) or CodeSandbox (https://codesandbox.io/s/new) example below:
What is the expected behavior?
Just like the docs suggest:
Accordingly, I have implemented a Babel-plug-in that does exactly that; see babel-plugin-react-persist. Given the code snippet above, the plug-in should generate the following output:
The plug-in will:
useCallback()
automatically when a function is created.useMemo()
automatically when a value is assigned.I don't see however how can
useEffect()
be inferred automatically and if it's a good idea. The plug-in is not a feature request directly for React, but since it's stated in the docs I thought maybe it can be useful somehow. Maybe it can potentially be included as part ofcreate-react-app
? Would like to hear your thoughts about it. An alternative solution is suggested at #14406, but I don't see why do this at runtime when everything can be done ahead of time and save processing power.Which versions of React, and which browser / OS are affected by this issue? Did this work in previous versions of React?
React 16.8-alpha (hooks)
The text was updated successfully, but these errors were encountered: