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

estafette

v1.0.18

Published

Estafette helps you write code a bit faster

Downloads

137

Readme

Installation

With yarn:

yarn add estafette

With npm:

npm install estafette

useRequest(initialValues)

Hook function for fetching data. Every request will save data or validation errors and turn on/off loading.

Arguments

initialValues (Object): an object with initial values

const estafette = useRequest({
  loading: true,
  data: { title: 'Initial title' },
  errors: { title: 'This field is required' },
});

Returns

  • request a function which executes another callback function, stores request data, switches on/off loading and catches errors
  • data stores all data from callback function
  • errors catches all errors from callback function
  • loading stores loading state
import React, { useEffect } from 'react';
import axios from 'axios';
import { useRequest } from 'estafette';

const App = () => {
  const { request, data, loading, errors } = useRequest();

  useEffect(() => {
    request(axios.get('https://jsonplaceholder.typicode.com/todos/1'));
  }, []);

  if (loading) {
    return <span>Loading ...</span>;
  }

  return (
    <div>
      <h1>Title: {data.title}</h1>
    </div>
  );
};

Concat data in request(data, params)

  • request(fn: Function, { concat: boolean | string }) from useRequest as the second argument accepts object params with concat which can be a boolean, it means every new data from callback will concatinate with current data. Also, it can be a string in case when necessary to concatinate just some property from object in data.
import React, { useState, useEffect } from 'react';
import axios from 'axios';
import { useList, useRequest } from 'estafette';

const App = () => {
  const [page, setPage] = useState(1);
  const { request, data, loading } = useRequest();

  useEffect(() => {
    request(axios.get('https://jsonplaceholder.typicode.com/posts'), { concat: page > 1 });
  }, [page]);

  const onChangePage = () => setPage(page + 1);

  return (
    <>
      <ul>
        {useList(data, (item) => (
          <li>{item.title}</li>
        ))}
      </ul>

      <button onClick={onChangePage}>{!loading ? 'View more' : 'Loading'}</button>
    </>
  );
};

Skip loading or errors steps in request(data, params)

In the code bellow with additional parameters for request function we can switch off loading toggle and prevent errors reseting. Can be useful for seamless loading or for keeping validation errors in a form.

import React, { useState, useEffect } from 'react';
import axios from 'axios';
import { useList, useRequest } from 'estafette';

const App = () => {
  const [page, setPage] = useState(1);
  const { request, data, loading } = useRequest();

  useEffect(() => {
    request(axios.get('https://jsonplaceholder.typicode.com/posts'), { toggleLoading: false, resetErrors: false });
  }, [page]);

  return (
    <>
      <span>{errors.details}</span>

      <ul>
        {useList(data, (item) => (
          <li>{item.title}</li>
        ))}
      </ul>
    </>
  );
};

useList(data, renderItem)

Hook function for rendering list of data. Every item will be memoized and updated only when their data changes.

Arguments

  1. list (Array): Data
  2. render (Function): Render function which will be called for every item in list
<ul>
  {useList(['a', 'b', 'c', 'd'], (item, i) => (
    <li>
      {i + 1}. {item}
    </li>
  ))}
</ul>

Use direct in JSX

You can get the error Uncaught Error: Rendered fewer hooks than expected. This may be caused by an accidental early return statement. if you show or hide HOOK after first rendering, for this case you have to save into a variable, like this:

const renderList = useList(['a', 'b', 'c', 'd'], (item, i) => (
  <li>
    {i + 1}. {item}
  </li>
));

return <ul>{renderlist}</ul>;