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-search-cache

v1.0.2

Published

A smart searchbar with data caching for offline searches

Downloads

6

Readme

React Search Cache 💾

GitHub license Build Status CodeFactor PRs Welcome npm

A smart search component which caches your searched data in localstorage.The searched data can be retrived even there is no network connection.

Ideal for saving expensive network requests and for offline Webapps.

Basic Usage

Just import the <Searchbar > component and pass it the data you want it to persist. It renders two elements input element and a search button.

import React, { Component } from 'react';
import Searchbar from 'react-search-cache';
import axios from 'axios';
import SearchResults from './SearchResults';

class App extends Component {
  state = {
    apiData: {},
    dataToRender: {}
  };

  handleSearch = searchTerm => {
    const ROOT_URL = `http://api.openweathermap.org/data/2.5/forecast?appid=a3cb99e4a46d3c130772308d454ee522`;
    const url = `${ROOT_URL}&q=${searchTerm}`;
    axios
      .get(url)
      .then(response => {
        if (response && response.data) {
          this.setState({ apiData: response.data });
        }
      })
      .catch(error => {
        console.log(error);
      });
  };

  handleDataToRender = data => {
    this.setState({ dataToRender: data });
  };

  render() {
    return (
      <div className="App">
        <header className="App-header">
          <Searchbar
            onSearch={this.handleSearch}
            apiData={this.state.apiData}
            handleDataToRender={this.handleDataToRender}
            styles={CustomStyles}
          />
          <SearchResults data={this.state.dataToRender} />
        </header>
      </div>
    );
  }
}

const CustomStyles = {
  input: {
    background: 'transparent',
    borderWidth: '1px',
    lineHeight: '1.5',
    height: '43px',
    width: '500px',
    borderRadius: '5px',
    fontSize: '17px'
  },
  button: {
    fontSize: '19px',
    paddingTop: '0px',
    paddingBottom: '0px',
    color: '#ffffff',
    paddingLeft: '18px',
    paddingRight: '18px',
    borderRadius: '4px',
    border: '0px',
    background: '#FF5A5F',
    height: '45px',
    textAlign: 'center',
    display: 'inline-block',
    whiteSpace: 'nowrap',
    marginLeft: '7px'
  }
};

export default App;

Props

Only a few of them!

  • onSearch: function: You have to handle this function to fetch data from the api, you will receive searchterm in the argument of this function.The data received from the api should be passed in the local state of the component you are including the search bar.
  • apiData: object: Pass the data received from the api into this object.
  • handleDataToRender: function: You will receive the actual data to render corresponding the searchterm inside this function , handle this in a function to update local state with the new data.
  • styles: object: (optional) You can optionally pass the custom styles to override the default styles.

Author