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-data-distributor

v1.0.1

Published

A minimalist and unopinionated framework for distributing static data to React components.

Downloads

3

Readme

React Data Distributor

A minimalist and unopinionated framework for distributing static data to React components.
Build Status npm version dependencies Status

With React Data Distributor, you bring your own data and formatting functions and they are passed down the component tree to where you need them. It is a minimal wrapper on the React context api, designed for static(ish) data. React Data Distributor is lightweight, coming in at 1.6KB transpiled, minified, and gzipped. It is powerful tool for translating, theming, content distribution, and is the perfect tool when complex state management libraries are overkill. It has no dependencies except for a peer dependency on React >= 16.3.

Install

Install via your favorite node dependency manager

yarn add react-data-distributor
npm install react-data-distributor

API

Distributor

A top level data provider

Configs

  • data: {object<object|string|number>} - the data to distribute as key:value pairs, may be nested objects
  • formatters: {object<function>} - optional - an object of formatting functions that take one parameter, a data value, and return a string. You may optionally define a special formatter named "default", that will be called any time a Parcel's value function is called without a formatter function. If no default formatter is defined, the value will be returned as a string.

Customer

A low level data consumer

Configs

  • render: {function} - a render prop that accepts a single argument, an object of parcels, and returns a React component

Parcel

A single data value

Properties

  • rawValue: {string|number} - the actual data value

Methods

  • value: {function} - returns the formatted data value as a string. Accepts one argument, the name of a formatting function as defined in the formatters config of Distributor.

Example

Below is a real world example using react-data-distributor to provide a simple translation mechanism to a React app.

Create the Distributor

import React from 'react';
import ReactDOM from 'react-dom';
import { Distributor } from 'react-data-distributor';
import App from './app';

// define a translations object, that maps
// keywords to phrases in our target languages
const translations = {
  en: {
    lang: 'english',
  },
  es: {
    lang: 'español',
  },
};

// extract the user's language settings from the navigator,
// defaulting to english if we couldn't find it.
let language = (typeof navigator !== 'undefined' && (
  navigator.languages ? navigator.languages[0] : navigator.language
)) || 'en';

// the language tag that the user holds might be more specific than
// what we support in our translations, i.e. en-US. In that case,
// try to find a match using the first part of the language tag.
// fall back to english if we still can't find a match.
if (!translations[language]) {
  const simplifiedLangTag = language.split('-')[0];
  language = translations[simplifiedLangTag] ? simplifiedLangTag : 'en';
}

// define any formatters that we want to use on our data
const formatters = {
  capitalize: (str) => {
    const words = (str || '').split(' ');
    return words.map(w => `${w.substr(0, 1).toUpperCase()}${w.slice(1)}`).join(' ');
  },
  default: str => `${(str || '')}`.toLowerCase(),
};

// render the application with Distributor somewhere near the top of component
// hierarchy. It might make sense to instead have multiple distributors at the 
// page level, depending on the situation.
ReactDOM.render((
  <Distributor
    data={translations[language]}
    formatters={formatters}
  >
    <App />
  </Distributor>
), document.getElementById('root'));

Create the Customer

import React, { Fragment } from 'react';
import { Customer as C } from 'react-data-distributor';

// render your component using the render props pattern.
const Component = () => (
  <C render={parcels => (
    <Fragment>
      <div>Default Formatter: {parcels.lang.value()}</div>
      <div>Custom Formatter: {parcels.lang.value('capitalize')}</div>
      <div>Raw: {parcels.lang.rawValue}</div>
    </Fragment>
  )} />
);

export default Component;

Changelog

1.0.1 — Aug 14, 2018

  • Unit testing and CI setup

1.0.0 — Aug 13, 2018

  • Initial release