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

@amongotham/react

v1.0.0

Published

1. useModal

Downloads

3

Readme

resto-react

COMPONENTS

  1. useModal
  • The concept of this component is to render a modal-like components both in Native, Web and other React platforms and prevents unnecessary re-renders accross the app.

USAGE

import { ModalProvider, useModal } from "@resto-react";

function EntryPoint() {
  return (
    <ModalProvider>
      <App/>
    </ModalProvider>
  )
}

function ProductLists() {
  const toastMessage = useModal(MyOwnCustomToastMessage);
  const [products, setProducts] = React.useState(null);

  React.useEffect(() => {
    (async function fetchApi() {
      try {
        const data = await getProducts();
        setProducts(data)
      } catch (err) {
        /**
         * here by the showing the toastMessage,
         * we render the *MyOwnCustomToastMessage* only in the React tree.
         * This prevents re-renders in this component because conceptually
         * only the MyCustomToastMessage should be rendered.
        */
        toastMessage.show({ message: "Problem fetching Products" })
      }
    })()
  }, []);

  if (!products) return <Loading />

  return ...
}

function MyOwnCustomToastMessage(props) {
  /* here the modal itself pass hide prop implicitly to hide the component.*/
  const { hide } = props;

  React.useEffect(() => {
    // perform animation so that it feels like a toast
  }, []);

  function onClose() {
    // perform closing animation
    closingAnimationFn();
    hide(); // hide provided by the modal removes the component from the React tree.
  }
}

PLEASE NOTE

useModal doesn't care much about how one might render the component style-wise. It only cares about mounting and unmounting the component from the React tree.

  1. useToggle
  • A hook used to toggle a boolean, useful for state that require toggle like switch, accordian, dropdown etc

Usuage

const [on, toggle, setTrue, setFalse] = useToggle();
  1. useSorter
  • A hook used to sort the items

Usuage

import {
  useSorter,
  descendingStringComparator,
  ascendingStringComparator,
  ascendingNumberComparator,
  descendingNumberComparator,
} from '@resto/react';

const items = [
  { id: 4, name: "Z", price: 90 },
  { id: 1, name: "A", price: 120 },
  { id: 2, name: "B", price: 20 },
]


// let's define the sortStates outside the scope of component
// you could create your own custom comparator
const defaultSortStates = [
  {
    sortKey: 'price',
    ascComparator: ascendingNumberComparator,
    descComparator: descendingNumberComparator,
    sortOrder: null, // null, if the items are not supposed to be sorted at the initial mount
  },
  {
    sortKey: 'name',
    ascComparator: ascendingStringComparator,
    descComparator: descendingStringComparator,
    sortOrder: 'asc',
  },
];

function MyComponent() {
  const [getSortByKey, updateSortByKey, sortedItems] = useSorter(items, defaultSortStates);

  return (
    <section>
      <button onClick={() => updateSortByKey("name", "asc")}>
        Sort By Name (asc)
      </button>
      <button onClick={() => updateSortByKey("name", "desc")}>
        Sort By Name (desc)
      </button>

       <button onClick={() => updateSortByKey("price", "asc")}>
        Sort By Price (asc)
      </button>
      <button onClick={() => updateSortByKey("price", "desc")}>
        Sort By Price (desc)
      </button>

      <ul>
        {sortedItems.map(item => <li key={item.id}>{item.name}</li>)}
      <ul>
    </section>
  )
}

PLEASE NOTE: You could create your own comparators and pass it on as the defaultSortStates

CONSIDERATIONS

  1. Formatting

Formatting in 2021, is a least thing one should worry about. To make it easy and consistent across project. Please install

prettier

in your IDE ( be it Vscode, Webstorm or Atom).

Enable prettier so that it formats automatically in default.