-
-
Notifications
You must be signed in to change notification settings - Fork 634
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
useState hook #1453
Comments
No, not yet at least |
@Havunen |
preact just implemented a hook compat version https://github.com/developit/preact/tree/master/hooks i looked on code, maybe can somebody inspired to add this support to inferno? I would like to use amazing react-spring who use hook system =) |
It seems that hooks aren't that hard to implement. All Inferno hook points are clearly defined, we just need to expose them on an options object, and copy paste, wire up the Preact's file. |
Hi! I was interested in working on this issue if this is still open and good to go for |
@BaibhaVatsa Yes, I'd really like to see this implemented. |
Since I am new to this project, would you like to give me any pointers regarding the implementation apart from the link to preact implementation? |
This is a very basic, bare bones setup for hooks (i was coding it inside tgstation repo, which is less than ideal, but this diff will give you the basic idea) : I think these are all the hook points necessary to implement the top level React Hooks API in Inferno. You can use the Then we have this (preact hooks), what i think is the cleanest implementation of react hooks out there: The main stopper is that you can't use these hooks as is, because:
They also have this stinky bit, this is where I personally got stuck: They have a commit queue and a I'm not sure if i have give you more hints on how to approach this issue, I have only scratched the surface myself. If you have questions, contact me in inferno's Slack, I'll be glad to guide you and help where I can. |
Disclaimer: I'm one of the maintainers of Preact.
The "stinky" bit you're referring to is the place where we invoke and process all pending effects that have been scheduled by various hooks. In our case You may be wondering why we mix those and the reason is simply to save some bytes. And yes, this allows hooks to be used in class components! We don't really advertise that though 👍 |
Epic! |
@marvinhagemeister to be clear, I don't think that code is bad in context of Preact, it's just written in a way that makes it less portable, at least that's how it appears. Maybe you may have a suggestion how to unwrap that functionality, or point out some specifics which may be useful to @BaibhaVatsa. |
Yeah, the code was specifically written for Preact and we never intended it to be used with other frameworks. I doubt that we'll change that as it would likely result in negative effects on our byte count. Some of us are experimenting with bringing hooks into the core itself, thus making it likely even less portable. You're best bet is probably to copy that code and replace the integration to what's best for inferno. Our test suite should be pretty portable as we only assert against the rendered DOM. |
I built something similar to hooks for a standalone library I built recently. It was pretty handy, so I decided to bundle up the hooks functionality into its own library xferno. It's not quite React hooks, as it requires you to wrap your components in an import { xferno, useState } from 'xferno';
const Counter = xferno(() => {
const [count, setCount] = useState(0);
return (
<button onClick={() => setCount(count + 1)}>{count}</button>
);
}); |
OK. I published import { useState } from 'xferno';
function Counter() {
const [count, setCount] = useState(0);
return (
<button onClick={() => setCount(count + 1)}>{count}</button>
);
} It's got some hackery going on under the hood, but if anyone on the core team likes it enough, I'd be down with renaming it to |
I have the same concern. I put together two (very simple) perf files |
@chrisdavies I did some A/B tests on /tg/station (which uses IE11 to render UIs), one with just inferno, and one with xferno. Identical code, no hooks, just to measure what amount of overhead xferno was generating during re-renders. Screenshot of the interface I was testing. It uses a lot of simple html tags with known structure, inferno is known to optimize that very well, plus a pair of SVG curves.
That's a pretty considerable slowdown. Screenshot of another interface. In this UI, only high level components were used (such as Xferno was only marginally slower, by around 0.5ms, which is insignificant. So, perhaps this may give you an idea where this slowdown originates from, even though this is only a surface level test. |
if @Havunen could have a look on xferno will be great to create inferno-hooks in optimal way.. . I really want to try hook in my next application, as all code seems to work with hooks now.. (Apollo ..Etc) which it seems to give much better coding practice. |
I wonder if one could implement hooks with good performance via compiling functional components that use hooks into class components at runtime on the first usage. Since all hooks need to run each render, you have guarantees that even for nested hooks, it should be possible to track all the calls. If you can compile the dynamically created functions into functions on a prototype that aren't re-created each call, it should be faster than React Hooks. Example: const TextBox = ({text}) => {
const isDirty = Inferno.useRef(false);
const markDirty = Inferno.useCallback(() => {isDirty.current = true;}, [isDirty]);
return <textarea onChange={markDirty} value={text} />
} This would compile into something like: class TextBox extends Inferno.Component {
constructor(props) {
super(props);
this.$$ref0 = Inferno.createRef(false);
this._childProps = {onChange: this.$$cb0, value: this.props.text};
}
$$cb0() {
this.$$ref0.current = true;
}
render() {
this._childProps.text = this.props.text;
this._childProps.$$cb0 = this.$$cb0;
return createElement(textarea, this._childProps);
}
} Probably the hardest part of this approach is correctly associating variable names with hook usages, without bloating the bundle with something like |
The real slowdown in xferno is not caused by the overhead of hooks, but by some small bug that negates child/type flags optimization on vnodes, which should be easy to fix. |
Well lets start to fix that and create a real inferno-hooks.. if one moderator can have a look... |
@stylemistake do you know where that issue is, by approximation? I'm interested in designing hooks for inferno, but I'm not quite sure where this bug maybe |
@ScottAwesome I have forgot already, but this is probably the piece that creates the slowdown: https://github.com/chrisdavies/xferno/blob/master/src/xferno.jsx#L217-L223 VNodeFlags are being forced to ComponentUnknown, because well, the target component is abstracted away by the HookComponent and Inferno is required to go through all possible code paths. VNodeFlags are normally calculated at compilation time by the babel-inferno-plugin, because it knows the structure of all nodes, and it tries to bake this information into createVNode calls. HookComponent itself is not that thick, but it might contribute just a little bit, because it is still a wrapper. |
Thanks for the information, @stylemistake ! So, this is something that really should be optimized in core rather than as an addon library I imagine? Would that be something that would be acceptable? I wonder how hard it would be to simply extract some of the lifecycle method logic (since you can use it on function components too) and expose that as hooks in that case. I'd like to do something work with that if we can reach some basic clarification on how we would want that to look, so hooks can be baked into inferno directly |
I assume that both ways are fairly complex. In case of xferno, a lot of work has already been done, but figuring out how to optimize it might be hard. But if you choose to reimplement everything in core, you will spend more time, but might end up with a better implementation. I have no preference one way or the other.
Here is what I think is the most optimal way of implementing hooks:
Check stylemistake/tgstation@d1b1385. I didn't do much in my branch, but I did expose various internal hook points, which might be of interest. |
Hey bro looking today at the actual code of xferno its seems that need some change This wrapper ask to update every component by default even if no-one hook change the state (shouldUpdate is by default true) something like there is some issue in the code i think shouldUpdate from hook is never checked here for the rerender, only props then why not change
to
else the renderchild will be called anyway twice so twice more render.. also with new inferno docs 5a5ff08 I m not good enough in benchmarking to recreate that, if @chrisdavies could make these changes and check the new benchmark will be nice... and with the new inferno 8.0 maybe? inferno/packages/inferno/src/core/implementation.ts Lines 90 to 97 in 544bacd
it s resolved directly to VNodeFlags. ComponentClass Thks |
|
ok i did some work done on xferno. > > @ScottAwesome , @stylemistake , @chrisdavies for me now xferno hooks work with my gatsby application after changing a bit the code source of xferno the code of fast-refresh-overlay use usememo, useref, usestate, useReducer, tested and the error portal finally well display smooth with the useLayouteffect :) (the code of gatsby fast-refresh is here)' useLayouteffect code was mostly taken from preact Usestate is rewritted to work with usereducer i did add some tests when a dom element is destroyed that did produced some problem and infinite loop with usestate. I personaly use the code in development because gatsby use these hooks in his core now, but in production i don t use hooks so i won t use xferno for production (certainly bit slower because the component hook wrapper) for those interested my xferno version in the branch here feel free to ask how to install it with inferno (i use source code and resource aliases in webpack) xferno forked working well and tested with inferno7 in a webpack-dev-middleware SSR environnement, TODO babel-plugin-inferno is used too |
Would this "xferno" get merged into upstream? And what would be its effect on performance? ping @Havunen @simonjoom |
Any updates? I want to refactor my project using Inferno, but I found that there is no |
Inferno is already very useful in projects which require high performance but a rewrite is really costly timewise. |
Ultimately the useState hook api is state management API. You could use any other state management with inferno to store information |
Using xferno is not good for production it slowing down inferno. Far those interested by my fork i just did update the readme to clarify a bit the installation. |
well the react hooks are done by react and used by many library.. not really nice to reinvent the wheel all the time. |
Usestate do work with my fork |
Inferno is not React, hooks are React-specific API. Inferno has its own API which is similar to the API React uses but not the same. No other library uses React hooks, because they are part of React. Inferno's API is most similar to an older version of React's API, Inferno does not promise to be React-compatible, but React-like. If you really can't live without React hooks just use React or Preact instead. Preact promises React compatibility. I would recommend you try to do without React hooks. |
Does inferno compatible with the new react hook useState?
The text was updated successfully, but these errors were encountered: