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

pwb-fp-react

v1.0.1

Published

Functional tools for React components

Downloads

3

Readme

Functional Helpers for React

A work in progress, experimental, library for functional helpers to React components.

partial(Component, partialProps)

function MyComponent (props) {
  return <div>{props.foo}{props.children}</div>;
}
const Partialed = partial(MyComponent, {
  bar: 'bar',
  a: 1,
  children: [
    <h1>Hello, world</h1>
  ]
});
render(
  <Partialed foo='foo' a='2'>
    <p>My message</p>
  </Partialed>
);

Returns

<MyComponent foo='foo' bar='bar' a='2'>
  <h1>Hello, world</h1>
  <p>My message</p>
</MyComponent>

partialRight(Component, partialProps)

Same as partial, but (first) partially applied arguments take precedence. Useful for the times you want to guarantee that a component has a property set.

function MyComponent (props) {
  return <div>{props.foo}{props.children}</div>;
}
const Partialed = partialRight(MyComponent, {
  bar: 'bar',
  a: 1,
  children: [
    <h1>Hello, world</h1>
  ]
});
render(
  <Partialed foo='foo' a='2'>
    <p>My message</p>
  </Partialed>
);

Returns

<MyComponent foo='foo' bar='bar' a='1'>
  <p>My message</p>
  <h1>Hello, world</h1>
</MyComponent>

memoizeish(Component)

Almost like memoize, but more as a 1-step Markov chain, not with history. Utilizes a smart default shouldComponentUpdate from Omniscient.js.

const Memoized = memoizeish(function MyComponent (props) {
  return <div>{props.foo}</div>;
});

render(<Memoized foo='bar' />);
render(<Memoized foo='bar' />); // not updated
render(<Memoized foo='bar' />); // not updated

wrap(value, [wrapper=identity])

function MyComponent (props) {
  return <h1>{props.foo}</h1>;
}
const Section = wrap(MyComponent, function (Comp, props) {
  return (
    <section>{Comp(props)}</section>
  );
});

render(React.createElement(Section, { foo: 'foo' }));

Returns

<section><h1>foo</h1></section>

curry(Component, arity = 1, initialProps = { })

function MyComponent (props) {
  return <div>{props.foo}{props.bar}</div>;
}
const Curried = curry(MyComponent, 2, { a: 1 });
const Curried2 = Curried({ bar: 'bar' }); // Curry nr. 1
const Curried3 = Curried2({ // Curry nr. 2
  baz: 'baz',
  bar: 'foo',
  a: 42
});
render(React.createElement(Curried3, { foo: 'foo' }));

Returns

<MyComponent a='42' bar='foo' baz='baz' foo='foo' />

With propTypes

Instead of defining the arity yourself you can set required proptypes on your component and automatically define arity. Curried components with proptypes will terminate currying when all required props are provided.

function MyComponent (props) {
  return <div>{props.foo}{props.bar}{props.baz}</div>;
}
MyComponent.propTypes = {
  foo: React.PropTypes.string.isRequired,
  bar: React.PropTypes.string.isRequired,
  baz: React.PropTypes.string
};

const Curried = curryRight(MyComponent);
const Partial1 = Curried({ foo: 'foo' });
const Partial2 = Partial1({ baz: 'baz' });
const Partial3 = Partial2({ bar: 'bar' });

render(React.createElement(Partial3, { foo: 'override' }));

Returns

<MyComponent bar='bar' baz='baz' foo='override' />

curryRight(Component, arity = 1, initialProps = { })

curryRight is to curry as partialRight is to partial. The former passed properties takes precedence of the latter. Useful for the times where you want to be sure that a component has properties set.

function MyComponent (props) {
  return <div>{props.foo}{props.bar}</div>;
}
const Curried = curryRight(MyComponent, 2, { a: 1 });
const Curried2 = Curried({ bar: 'bar' }); // Curry nr. 1
const Curried3 = Curried2({ // Curry nr. 2
  baz: 'baz',
  bar: 'foo',
  a: 42
});
render(React.createElement(Curried3, { foo: 'foo' }));

Returns

<MyComponent a='1' bar='bar' baz='baz' foo='foo' />

(note the a and bar properties)

With propTypes

Instead of defining the arity yourself you can set required proptypes on your component and automatically define arity. Curried components with proptypes will terminate currying when all required props are provided.

function MyComponent (props) {
  return <div>{props.foo}{props.bar}{props.baz}</div>;
}
MyComponent.propTypes = {
  foo: React.PropTypes.string.isRequired,
  bar: React.PropTypes.string.isRequired,
  baz: React.PropTypes.string
};

const Curried = curryRight(MyComponent);
const Partial1 = Curried({ foo: 'foo' });
const Partial2 = Partial1({ baz: 'baz' });
const Partial3 = Partial2({ bar: 'bar' });

render(React.createElement(Partial3, { foo: 'override' }));

Returns

<MyComponent bar='bar' baz='baz' foo='foo' />

(note the foo property)

flow(...components)

function Div (props) {
  return <div>{props.children}</div>;
}
function Section (props) {
  return <section>{props.children}</section>;
}
function H1 (props) {
  return <h1>{props.children}</h1>;
}
function Italic (props) {
  return <em>{props.children}</em>;
}

const Composed = flow(Div, Section, H1, Italic);
render(
  <Composed>Hello World!</Composed>
);

Returns

<div><section><h1><em>Hello World!</em></h1></section></div>

flowRight(...components)

function Div (props) {
  return <div>{props.children}</div>;
}
function Section (props) {
  return <section>{props.children}</section>;
}
function H1 (props) {
  return <h1>{props.children}</h1>;
}
function Italic (props) {
  return <em>{props.children}</em>;
}

const Composed = flowRight(Italic, H1, Section, Div);
render(
  <Composed>Hello World!</Composed>
);

Returns

<div><section><h1><em>Hello World!</em></h1></section></div>