Prehook is now Reflex
Prehook becomes Reflex, a tiny V-dom library with factory phase functions ✨
This is a proof of concept of a React Hooks pattern but for Preact.
This is an early Proof of concept for a Hook implementation, a bit different than the React approach.
- This is for Preact and the goal is to maintain minimal output size, with maximal code flexibility and execution performaces.
- I'm not 100% OK with the React Hooks proposal. To much hooks declared inside each render could, I think, lead to performances issues. These performaces issues does not exist with the Class system, so this is a drawback added by hooks we need to avoid.
React has a solution for patching performances with
useMemoization
anduseCallback
hooks but this is not needed with this implementation. - Also, the "don't use hooks in conditions" is a problem to me. It shows that there is to much "magic" going on under the hood. Which is not aviced when you dive into a new lib / way of thinking. People need to understands how it works so it's not frustrating and easy to learn. And to me, linters should not be mandatory to use a lib, syntax have to be expressive and clear.
For now the POC is less than 3KB (not GZip), added to the Preact (10KB not GZIP) core with useProps
, useState
, useEffect
and a better useRef
.
This example is 16KB (not GZip) with Preact + Prehook + working example.
I plan to add useReducer
and useContext
as separated files (like useRef
).
- Components are still pure functions, but they have 2 phases : Factory and Render phases.
- Components are not re-executed entierly at each render, they return a render function.
- Hooks are initialized only at component build time, for better performances.
- Hooks can be inside conditions as long they are inside component's factory phase.
useState
have a slightly different API. With more functional approach.- Ref's are more usable with solo refs and multi-refs.
useEffect
update condition are on top of the declaration, before implementation. Small diff but cleaner and more readable code.- Props are gathered and defaulted with
useProps
hook.
No need for React's useMemo and useCallback, which help keep our apps fast and simple.
This is built with Typescript for now and will be pre-compiled for npm when (if) ready.
I know that Preact's author is working on a Hooks implementation. I also know that Preact goal is to mimic React API's so there is no much chances that is POC is used at anytime by anybody, this is just a proposal :)
Thanks !
The more important aspect of the lib, is the Factory phase and Render phase.
- Factory phase scope leak through all the component, but is called once.
- Render phase is called at each updates.
( There is an example of effect watching a prop below )
Here is a custom hook implementation example :
And two usages of the same hook :
If you just want to check implementation without installing : Example :
The lib :
Packages are installed automatically
Will watch files and check Typescript.
node solid
ornpm run dev
node solid production
ornpm run build