use-server-effect
v1.0.0
Published
useServerEffect is a serverside-rendering friendly wrapper around the useEffect React hook. This package provides a browser shim for compatibility in client-side react.
Downloads
4
Readme
useServerEffect
useServerEffect is a serverside-rendering friendly wrapper around the useEffect React hook. This package provides a browser shim for compatibility in client-side react.
For serverside-rendering, you would intercept the require call for this package, and re-render the component after the returned Promise resolves.
This is implemented in Shayu, a static site generator.
The Rules of Hooks still apply.
License, donations
GPL-3.0. If you want to support my work, you can:
Example
see example.js
in the repo:
const React = require("react");
const useServerEffect = require("use-server-effect");
function Test() {
let [thing, setThing] = React.useState("initial");
useServerEffect(() => {
return new Promise((resolve) => {
setTimeout(() => {
setThing("changed");
resolve();
}, 1000);
})
})
return thing;
}
API
requireTranspile(effect)
- effect: function that returns a Promise, resolving after the state(s) are set
Changelog
v1.0.0 (🏳️🌈 June 19, 2020)
- working implementation
v0.0.1 (🏳️🌈 June 16, 2020)
- basic untested implementation, reserve package name to safely override in code