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-spotify-api

v3.0.0

Published

A component library that helps you interact with the Spotify API

Downloads

101

Readme

react-spotify-api

A component library that helps you interact with the Spotify API

Demo

NPM Build Status Dependencies Dev Dependencies Peer Dependencies Codecov npm bundle size (minified) GitHub PRs Welcome

Documentation

Features

  • Components for most of Spotify's data types that pass data through render props
  • Hooks for most of Spotify's data

Roadmap

  • [x] Pass Spotify data with render props
  • [x] Use React.Context to pass the access token down the component tree
  • [x] Hooks!
  • [x] A demo page that uses this library - available here!
  • [x] Load more data support (infinite scrolling) - current works for some of the data types
  • [ ] TypeScript support!
  • [ ] 100% code coverage
  • [ ] Hooks for all data types from Spotify's API
  • [ ] Hooks for using the Spotify Playback SDK

Version 3.0.0 Breaking Change

Before version 3.0.0 the parameters to props.children were passed in this order - data, loading, error. It is now passed as an object, so you would now use the Album component like this -

<Album id={...}>
  {({ data }) => {
    return <></>;
  }}
</Album>

As opposed to the previous versions where you would use the components like this -

<Album id={...}>
  {(data, loading, error) => {
    return <></>;
  }}
</Album>

This way you can choose which parameters you would like to have, and if you want just the error parameter you can omit the other two. This works well with the loadMoreData parameter, you don't need to write all 4 parameters if you just need some of them.

Installing

with npm

npm install --save react-spotify-api

with yarn

yarn add react-spotify-api

Wrapping your app with a Provider

in order to use the Spotify API you are required to send an access token (read more here) with every single http request, but the SpotifyApiContext provider does that for you!

Import

import { SpotifyApiContext } from 'react-spotify-api';

Wrap your app with it (all react-spotify-api components must have a SpotifyApiContext.Provider parent)

<SpotifyApiContext.Provider value={token}>
  <App />
</SpotifyApiContext.Provider>

You can now use all components without worrying about getting your access token!

Component usage

import React, { Component } from 'react';

import { SpotifyApiContext, Artist } from 'react-spotify-api';

function Example(props) {
  return (
    <SpotifyApiContext.Provider value={props.token}>
      <Artist id={props.id}>
        {({ data, loading, error }) =>
          data ? (
            <div>
              <h1>{data.name}</h1>
              <ul>
                {data.genres.map(genre => (
                  <li key={genre}>{genre}</li>
                ))}
              </ul>
            </div>
          ) : null
        }
      </Artist>
    </SpotifyApiContext.Provider>
  );
}

Hooks usage (assuming the ExampleHooks component is wrapped with the SpotifyApiContext.Provider)

import React from 'react';

import { useArtist } from 'react-spotify-api';

function ExampleHooks(props) {
  const { data, loading, error } = useArtist(props.id);

  return artist ? (
    <div>
      <h1>{artist.name}</h1>
      <ul>
        {artist.genres.map(genre => (
          <li key={genre}>{genre}</li>
        ))}
      </ul>
    </div>
  ) : null;
}

Data types

  • data - Each component has a link to the Spotify API endpoint where you can see the data model for that specific data type
  • loading - Boolean (true when loading and false when finished loading)
  • error - null when there are no errors but an object when there are - usually containing the error object received by the fetch api, so it looks something like: {status: 404, message: "Not Found"}

License

This project is licensed under the MIT License - see the LICENSE file for details

MIT © idanlo