FunHooks
Hooks for functions.
If you are a fan of React hooks and want to use them everywhere, this is the package for you.
Disclaimer - this is a WIP, don't use it in production. do use it to figure out what can be done.
What's in the box?
API
-
useFun
- a hook that allows you to use hooks in functions
-
useState
- a hook that allows you to create contextual state in functions
-
useEffect
- a hook that allows you to use effects in functions (effects run on the microtask queue)
-
useMemo
- a hook that allows you to use memoization in functions
-
useRef
- a hook that allows you to create refs in functions
-
useContext
- a hook that allows you to use context value in functions
-
useCallback
- helper hook that allows you to use memoize functions
-
createContext
- a function that allows you to create a context
-
mount
- a function that allows you to mount a function on a key
-
autoMount
- same as mount
but waits for all suspended functions to resolve
Functionality
Composition - compose functions like components
Suspense - suspend a function until all its dependencies are resolved
How does it work?
import { useState, useEffect, mount } from 'funooks';
function Model() {
const [count, setCount] = useState(0);
const [name, setName] = useState('John');
useEffect(() => {
console.log('count effect', count);
}, [count]);
useEffect(() => {
console.log('name effect', name);
}, [name]);
return {
setCount,
setName,
};
}
const model = mount(Model, []);
model.setCount(1);
model.setName('Will');
What's missing (currently)?
API
useReducer
- soon to be implementeduseImperativeHandle
- will be interesting to implement
Functionality
- unmounting - the next in queue to be implemented
- live model updates - will be interesting to implement
- aborting - will be interesting to implement
Docs
- context
- tests as examples
- what is mount key
- how to use the returned value
- useFun - how to use it with refs
- direct function calls vs useFun
- suspense - how it behaves with fallbacks
FAQ
Do I need React?
No, you don't. This package is not tied to React in any way. It just uses the same React's hooks API.
Is this a joke?
No, it's not. It's a serious package that allows you to use hooks in functions.
Should I use this?
No, you shouldn't. This is a joke package.
How to contribute?
Try to create interesting cases that will justify the existence of this package. If you have any ideas, please open an issue.