npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

reacting-function-hooks

v1.0.1

Published

Create React-like hooks for regular function.

Downloads

3

Readme

reacting-function-hooks

Create React-like hooks for regular function.

At First, i just want usingMemo to optimize performace. But why say no to more hooks.

Demo

Installation

npm install --save reacting-function-hooks

Usage

import reacting, {
  usingRef,
  usingMemo,
  usingState,
  usingEffect,
} from 'reacting-function-hooks';

const running = reacting(() => {
  const ref = usingRef(0);
  const [count, setCount] = usingState(0);

  const memoCount = usingMemo(() => {
    return count;
  }, []);

  usingEffect(() => {
    console.log('Do effect');
    return () => console.log('Clean effect');
  }, [count]);

  ref.current += 1;
  setCount(count + 1);
});

running();
running();

API

reacting

For React, we have react-node to cache hooks for Function Component. But we don't have it in regular function. So only thing we can do is use the function as a key to cache hooks.

import reacting, { usingMemo } from 'reacting-function-hooks';

const running = reacting(() => {
  const memorizedObject = usingMemo(() => {
    return {};
  }, []);
  
  return memorizedObject;
});

usingRef

usingRef returns a mutable ref object whose .current property is initialized to the passed argument (initialValue). The returned object will persist for everytime to run the function.

import reacting, { usingRef } from 'reacting-function-hooks';

const running = reacting(() => {
  const memoizedValue = useMemo(
    () => computeExpensiveValue(a, b),
    [a, b],
  );

  return memoizedValue;
});

Maybe it can replace global variables for function.

let count = 0;

const runCounter = () => {
  count += 1;
  return count;
};

Replace to :

import reacting, { usingRef } from 'reacting-function-hooks';

const runCounter = reacting(() => {
  const refCount = usingRef(0);
  
  refCount.current += 1;
  return refCount.current;
});

usingMemo

usingMemo returns a memoized value.

Pass a “create” function and an array of dependencies. usingMemo will only recompute the memoized value when one of the dependencies has changed. This optimization helps to avoid expensive calculations on everytime.

import reacting, { usingMemo } from 'reacting-function-hooks';

const running = reacting((a, b) => {
  const memoizedValue = useMemo(
    () => computeExpensiveValue(a, b),
    [a, b],
  );

  return memoizedValue;
});

usingState

usingState returns a stateful value, and a function to update it.

During the initial execute, the returned state (state) is the same as the value passed as the first argument (initialState).

But not like React, we cannot re-render or re-excute the function when setState.So for me, the hook is not so useful.

import reacting, { usingState } from 'reacting-function-hooks';

const running = reacting(() => {
  const [count, setCount] = usingState(0);
  
  setCount(count + 1);
  return count;
});

usingEffect

usingEffect accepts a function that contains imperative, possibly effectful code. And pass a second argument to useEffect that is the array of values that the effect depends on.

Often, effects create resources that need to be cleaned up, such as a subscription or timer ID. To do this, the function passed to useEffect may return a clean-up function.

For sync function, the effect will fire just after executing the function. For async function the effect will fire just after resolving the promise.

import reacting, { usingEffect } from 'reacting-function-hooks';

const running = reacting((a, b) => {
  usingEffect(() = {
    addToCache(a, b);
    return () => removeFromCache(a, b);
  }, [a, b]);
});

usingCallback

usingCallback returns a memoized callback.

Pass an inline callback and an array of dependencies. usingCallback will return a memoized version of the callback that only changes if one of the dependencies has changed.

import reacting, { usingEffect } from 'reacting-function-hooks';

const running = reacting((a, b) => {
  const memoizedCallback = useCallback(
    () => {
      doSomething(a, b);
    },
    [a, b],
  );
  
  return memoizedCallback;
});