Learn about our RFC process, Open RFC meetings & more.Join in the discussion! »

use-server-effect

1.0.0 • Public • Published

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:
Donate using Liberapay

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

Keywords

none

Install

npm i use-server-effect

DownloadsWeekly Downloads

3

Version

1.0.0

License

GPL-3.0

Unpacked Size

37.5 kB

Total Files

6

Last publish

Collaborators

  • avatar