An interface and a swappable implementation that can be used to implement universal custom hooks on top of hooks.
npm install @saasquatch/universal-hooksjavascript
// Set an implementation for the hooks
setImplementation(haunted)
// OR
setImplementation(React)
// OR
setImplementation(Preact)
// Use the hooks like normal
function useCounter() {
const [counter, setCounter] = useState(0);
const increment = () => setCounter((c) => c + 1)
const decrement = () => setCounter((c) => c - 1)
return {counter, increment, decrement}
}
`
$3
Rollup, Webpack and other bundlers allow compile-time replacement of implementations.
E.g. with the Rollup alias plugin https://github.com/rollup/plugins/tree/master/packages/alias
To swap in React
`js
entries: [
{ find: '@saasquatch/universal-hooks', replacement: 'React' },
];
`
To swap in Haunted
`js
entries: [
{ find: '@saasquatch/universal-hooks', replacement: 'haunted' },
];
`
API
Most common hooks are supported, except for useContext`.