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

react-solutions

v0.0.18

Published

React Solutions

Downloads

8

Readme

React-Solutions

A bunch of useful React components and hooks. I tried to bring other JS frameworks template syntax to React ecosystem.


How to install it?

npm i react-solutions

How does it work?

There are a lot of components and hooks that you can find the details as following:

Components

If/Else

If your condition returns true, then the If block renders your content.

<If condition={YOUR_CONDITION}>
    CONTENT
</If>

If your condition returns false, then the IfNot block renders your content.

<IfNot condition={YOUR_CONDITION}>
    CONTENTS
</IfNot>

Switch/Case

If you need a Switch component, you can use it as following.

let switcher = 'red';

<Switch on={switcher}>
  {/* This Case will render as the value matches the Switch's {on} prop */}
  <Case value="red">
    <p>Rendering the red case</p>
  </Case>
  <Case value="blue">
    <p>Rendering the blue case</p>
  </Case>
</Switch>;

// A Default Component is optional with a Switch block, if no Default is provided and
// no Case(s) match then the Switch will render null

switcher = 'green';

<Switch on={switcher}>
  <Case value="red">
    <p>Rendering the red case</p>
  </Case>
  <Case value="blue">
    <p>Rendering the blue case</p>
  </Case>
  <Default>
    {/* The Default will render as no value matches the Switch's {on} prop */}
    <p>Im what renders by default!</p>
  </Default>
</Switch>;

// Case(s) can be given an array of values as well which will all be taken into account when searching for a match in the Switch

switcher = 'yellow';

<Switch on={switcher}>
  <Case value="red">
    <p>Rendering the red case</p>
  </Case>
  <Case value={['green', 'yellow', 'blue']}>
    {/* This Case will render as one of the values match the Switch's {on} prop */}
    <p>Rendering the green, yellow and blue case</p>
  </Case>
  <Case value="blue">
    {/* fun fact! This will never render as blue is already apart of a previous Case */}
    <p>Rendering the blue case</p>
  </Case>
  <Default>
    <p>Im what renders by default!</p>
  </Default>
</Switch>;

Show/Hide

If your condition returns true, then the Show block shows your content. (display: visible)

<Show condition={YOUR_CONDITION} tag="div">
    CONTENT
</Show>

If your condition returns true, the Hide block hides your content. (display: none)

<Hide condition={YOUR_CONDITION} tag="div">
    CONTENT
</Hide>
  • tag wraps your content to show or hide it. the default is span.

Repeat

If you are looking for a Repeat component, it is for you!

<Repeat for={[1, 2, 3]}>
  {({ item, index }) => (
    <React.Fragment>
      <div>--- {item}</div>
      <Repeat for={[4, 5, 6]}>
        {({ item, index, length }) => (
          <>
            <div>------- {item}</div>
            <Repeat for={[7, 8, 9]}>
              {({ item, index, length, array }) => (
                <div>------------ {item}</div>
              )}
            </Repeat>
          </>
        )}
      </Repeat>
    </React.Fragment>
  )}
</Repeat>
  • item : the current value.
  • index : the index of the value in the array.
  • length : the length of the array.
  • array : the whole array.

The order of the parameters is important.

Try/Catch

TryCatch is intended to reduce boilerplate with handling errors that occur with rendering.

const renderComponentWithAPICallFailure = () => <SudoCode />;

<TryCatch
  try={renderComponentWithAPICallFailure}
  catch={(error) => (
    <span>
      An error has occurred! {error}
    </span>
  )}
/>;

// or without a catch. This will result in an error being thrown by the TryCatch component.
// It's best to provide a custom catch so you can do whatever specific logic you need to should something unexpected happen
<TryCatch try={renderComponentWithAPICallFailure} />;

Promise/Async

Promise component helps you to use promise (async) functions easier than before.

<Promise on={fetchData}>
  <Pending>
    <h1>Loading...</h1>
  </Pending>
  <Resolve>
    {(data) => {
      return (
        <div>
          <h1>Data Received</h1>
          <pre>{JSON.stringify(data, null, 4)}</pre>
        </div>
      );
    }}
  </Resolve>
  <Reject>
    {(error) => {
      return (
        <div>
          <h1>Error Generated</h1>
          <pre>{JSON.stringify(error, null, 4)}</pre>
        </div>
      );
    }}
  </Reject>
</Promise>
  • Pending : Initial state, neither fulfilled (resolved) nor rejected.
  • Resolve : The operation was completed successfully.
  • Reject : The operation failed.

Hooks

useDebounce

this hooks forces a function to wait a certain amount of time (millisecond) before running again.

const [term, setTerm] = useState('');
useDebounce(()=> {
  console.log(term); // debounced 1sec
  // call search api ...
  // return () => maybe cancel prev req 
}, 1000 ,[term]);

useThrottle

This hook ensures that a function is called at most once in a specified time period (millisecond).

const [count, setCount] = useState(0);
useThrottle(() => { console.log(count); }, 1000, [count]);