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

preact-context-provider

v1.2.1

Published

A generic <Provider /> for preact to put props into context

Downloads

210

Readme

preact-context-provider

npm Build Status

A generic <Provider /> for preact. It exposes any props you pass it into context. Also provides a merging variant <MergingProvider />, and utility functions provide and mergingProvide

Usage

Install it via npm:

npm install --save preact-context-provider

# or, for Preact X support
npm install --save preact-context-provider@preactx

Then import it and use:

import Provider from 'preact-context-provider';

let OBJ = { a: 'b' };

const App = (props, context) => {
	// now it's exposed to context!
	console.log(context.obj === OBJ) // true
};

render(
	<Provider obj={OBJ}>
		<App />
	</Provider>
);

Preact Version Support

By default, the master branch of this repo supports preact 9 and below, and is published in normal patch/minor/major releases to the latest tag in npm. Support for preact X (versions 10+ of preact) is handled in the preactX branch and are always published to the preactx tag in npm. When preact X obtains widespread adoption, the master branch of this project will support preact X and a new major version under latest tag will be published to in npm.

API

Table of Contents

Provider

Adds all passed props, children into context, making them available to all descendants.

To learn about context, see the React Docs.

Parameters

  • props Object All props are exposed as properties in context, except children

Examples

const Demo = (props, context) => {
  console.log(context);  // "{ a: 'b' }"
};
render(
  <Provider a="b">
    <Demo />
  </Provider>
);
//	"{ a: 'b' }"

// lower-level providers override higher providers for any keys that they define
render(
  <Provider a={key1: 'foo'} b={key2: 'bar'}>
    <Provider a={key3: 'buz'} >
      <Demo />
    </Provider>
  </Provider>
);
// "{ a: { key3: 'buz' }, b: { key2: 'bar' } }"

MergingProvider

Similar to Provider, but allows a special mergeProps prop to allow parent supplied context keys with the same name as those provided by the current MergingProvider to be deep merged, instead of replaced.

To learn about context, see the React Docs.

Parameters

  • props Object All props are exposed as properties in context, except children and mergeProps
    • props.mergeProps Array? If not supplied, all supplied props will be merged with keys already in context. If supplied as an array of strings, it will deep merge any prop names that are present in the array, and missing prop names be overriden by the child like Provider.

Examples

import Provider, { MergingProvider } from 'preact-context-provider';
const Demo = (props, context) => {
  console.log(context);  // "{ a: 'b' }"
};

// with mergeProps unspecified, all parent context keys are merged with the ones presently supplied, parent values taking precedence
render(
  <Provider a={key1: 'foo'}>
    <MergingProvider a={key2: 'bar'}>
      <Demo />
    </MergingProvider>
  </Provider>
);
// "{ a: { key1: 'foo', key2: 'bar' } }"

 // when mergeProps is an array, only specified keys are merged, non-specified keys get their value from current node
// in this example, only the 'a' context key is merged.  'b' is overwritten by the lower node
render(
  <Provider a={key1: 'foo'} b={key2: 'bar'}>
    <MergingProvider mergeProps={['a']} a={key3: 'baz'} b={key4: 'buz'}>
      <Demo />
    </MergingProvider>
  </Provider>
);
// "{ a: { key1: 'foo', key3: 'baz' }, b: {key4: 'buz'} }"

provide

Higher Order Component that wraps components in a Provider for the given context.

Parameters

Examples

import {provide} from 'preact-context-provider';
const Demo = (props, context) => {
  console.log(context.a);  // "b"
};
const ProvidedDemo = provide({a: "b"})(Demo);

ProvidedDemo.getWrappedComponent() === Demo; // true

render( <ProvidedDemo /> );

Returns Function A function that, given a Child component, wraps it in a Provider component for the given context.

mergingProvide

Higher Order Component that wraps components in a MergingProvider for the given context.

Parameters

Examples

import {mergingProvide} from 'preact-context-provider';
const Demo = (props, context) => {
  console.log(context.a);
};
const ProvidedDemo = mergingProvide({a: "b"})(Demo);

ProvidedDemo.getWrappedComponent() === Demo; // true

render( <ProvidedDemo /> ); // "b"

Returns Function A function that, given a Child component, wraps it in a MergingProvider component for the given context.