realar-project / realar

5 kB Advanced state manager for React

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

very low: external package realar-rehook

betula opened this issue · comments

realar-rehook
or
realar-rehooks

This module provides the possibility for reusing standard and custom React hooks. One function for creating a container with use-between hooks implementation inside.

import { useState, useEffect } from "react";
import { value, useLocal} from "realar";
import { rehook } from "realar-rehook';

class Logic {
  a = value("");

  b = rehook(() => {
    const [name, setName] = useState("");

    // And possible to realar logic here
    useEffect(() => (
      this.a.watch(setName)
    ), []);
    useEffect(() => this.a.set(name), [name]);
    // The useEffect should be run synchronously because we don't have mount/unmount component phase

    return {
      name,
      setName
    }
  });

  constructor() {
    this.b.val.setName("Joe");
  }
}

const Comp = () => {
  const logic = useLocal(Logic);
  return (
    <p>{logic.b.val.name} - {logic.a.val}</p>
  )
}

And I can implement React compatibility mode

const hooks = rehook.compat(() => {
  console.log(1);
  useEffect(() => console.log(3), []);
  console.log(2);

  // And support the useLayoutEffect as alias for useEffect
}); 

Declined