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

vedro

v1.0.5

Published

<p align="center"> <img width="400" style="" src="./Vedro.svg" /> </p>

Downloads

53

Readme

VEDRO

Ultrafast react store library, that prevent unnecessary renders.

Main features:

  • lightweight
  • easy to use
  • handles unnecessary re-renders

Why VEDRO?

The common problem of many store libraries and React.Context is unnecessary re-renders of child components upon every state change. With React.Context, all the children using this context will be rendered every time the provider value changes, even if they rely on unaffected parts of the store. And in high-load apps, performance issues become even more acute: unnecessary re-renders may cause notable FPS drops or even freeze the main thread.

VEDRO tackles this problem and triggers re-renders only when those portions of the store used in a component are updated.

Get started

VEDRO has all the necessary tools out-of-the-box. It provides createVedro function to create a context instance and a number of flexible hooks to access and mutate data.

mainStore.ts

import { createVedro } from "vedro";

interface IMainStore {
  count: number;
  str: string;
  str2: string;
}

const initialState: IMainStore = {
  count: 1,
  str: "Hello",
  str2: "This string does not change",
};

export const {
  Context: MainStoreContext,
  Provider: MainStoreContextProvider,
  useStore: useMainStore,
  useSelector: useMainStoreSelector,
  useDispatch: useMainStoreDispatch,
} = createVedro(initialState);

That's it. Context and hooks are created. Now we need to wrap our components with MainStoreContextProvider. For example, in our HomePage component:

HomePage.tsx

import { MainStoreContextProvider } from "mainStore"; // a file with the context
import { Counter } from "./Counter"; // some counter component
import { InputSection } from "./InputSection"; // some input component

export default async function HomePage() {
  return (
    <main >
      <MainStoreContextProvider state={{ count: 5 }}>
        <Counter />
        <InputSection />
      </MainStoreContextProvider>
    </main>
  );

Hooks

useSelector

useSelector works just like in any other similar libraries. It takes a selector callback and returns an object with a portion of the state specified in the selector callback:

const { count } = useMainStoreSelector((state) => ({ count: state.count }));

useDispatch

useDispatch returns a dispatch function for state mutation, which can be used in multiple ways:

const dispatch = useMainStoreDispatch();

// first example
const onClick = () => {
  dispatch("count", 7);
  dispatch("str", "new value");
};

// second example
const onClick = () => {
  dispatch({ count: 1, str: "new value" });
};

// third example
const onClick = () => {
  dispatch((state) => ({
    count: state.count + 1, // only a part of state we need to change is returned
    str1: "new value for str1",
  }));
};

As you might notice in the third example, the callback function passed to dispatch does not return the entire store – only the portion that changed.

The dispatch callback can also be async:

// in this example we have store with serveral fields:
// status - request status
// userData - user data fetched from server
const dispatch = useStoreDispatch();

const onClick = () => {
  dispatch(async (state) => {
    dispatch("loading", true);

    try {
      const userData = await getUserData(); // fetching user data
      dispatch("userData", userData);
    } catch (error) {
      console.log(error);
    }

    dispatch("loading", false);
  });
};

useStore

useStore hook returns the store instance. It can be used to access the store in non-React functions or classes.

Complete example

Now let's take a look at the hooks and their usage in components.

The Counter component:

Counter.tsx

import React from "react";
import { useMainStoreDispatch, useMainStoreSelector } from "mainStore";

export const Counter: React.FC = () => {
  const dispatch = useMainStoreDispatch();

  const { count } = useMainStoreSelector(({ count }) => ({ count }));

  return (
    <div>{ count }</p>
      <button
        onClick={() =>
          dispatch(({ count }) => ({ count: count + 1 }))
        }
      >
        Increment
      </button>
    </div>
  );
};

The InputSection component:

InputSection.tsx

import React from "react";
import { useMainStoreDispatch, useMainStoreSelector } from "mainStore";

export const InputSection: React.FC = () => {
  const dispatch = useMainStoreDispatch();

  const { str1 } = useMainStoreSelector(({ str1 }) => ({ str1 }));

  return (
    <div>
      <p>{name}</p>
      <input
        type="text"
        value={str1}
        onChange={({ target }) => dispatch("str1", target.value)}
      />
    </div>
  );
};

Now when we click the button in the Counter component, the count field gets updated in the main store. However, the only component to be re-rendered is the Counter. InputSection is not subscribed to the count field, thus, no re-renders will be triggered.