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-extras

v3.0.1

Published

Useful components and utilities for working with React

Downloads

6,286

Readme

react-extras

Useful components and utilities for working with React

Install

npm install react-extras

Usage

import React from 'react';
import {If} from 'react-extras';

const App = props => (
	<If condition={props.showUnicorn}>
		<div className="unicorn">
			🦄
		</div>
	</If>
);

API

autoBind(self, options?)

Automatically binds your React.Component subclass methods to the instance. See the autoBind.react() docs.

classNames(…input)

Conditionally join CSS class names together.

input

Type: string | object

Accepts a combination of strings and objects. Only object keys with truthy values are included. Anything else is ignored.

import {classNames} from 'react-extras';

classNames('unicorn', 'rainbow');
//=> 'unicorn rainbow'

classNames({awesome: true, foo: false}, 'unicorn', {rainbow: false});
//=> 'awesome unicorn'

classNames('unicorn', null, undefined, 0, 1, {foo: null});
//=> 'unicorn'

const buttonType = 'main';
classNames({[`button-${buttonType}`]: true});
//=> 'button-main'
import {classNames} from 'react-extras';

const Button = props => {
	console.log(props);
	/*
	{
		type: 'success',
		small: true
	}
	*/

	const buttonClass = classNames(
		'button',
		{
			[`button-${props.type}`]: props.type,
			'button-block': props.block,
			'button-small': props.small
		}
	);

	console.log(buttonClass);
	//=> 'button button-success button-small'

	return <button className={buttonClass}>…</button>;
};

<If>

React component that renders the children if the condition prop is true.

Beware that even though the children are not rendered when the condition is false, they're still evaluated.

If you need it to not be evaluated on false, you can pass a function to the render prop that returns the children:

import {If} from 'react-extras';

<div>
	<If condition={props.error} render={() => (
		<h1>{props.error}</h1>
	)}/>
</div>

Or you could just use plain JavaScript:

<div>
	{props.error && (
		<h1>{props.error}</h1>
	)}
</div>

<Choose>

React component similar to a switch case. <Choose> has 2 children components:

  • <Choose.When> that renders the children if the condition prop is true.
  • <Choose.Otherwise> that renders the children if has no <Choose.When> with true prop condition.

Note that even when the children are not rendered, they're still evaluated.

import {Choose} from 'react-extras';

<div>
	<Choose>
		<Choose.When condition={props.success}>
			<h1>{props.success}</h1>
		</Choose.When>
		<Choose.When condition={props.error}>
			<h1>{props.error}</h1>
		</Choose.When>
		<Choose.Otherwise>
			<h1>😎</h1>
		</Choose.Otherwise>
	</Choose>
</div>

Or you could just use plain JavaScript:

<div>
	{(() => {
		if (props.success) {
			return <h1>{props.success}</h1>;
		}

		if (props.error) {
			return <h1>{props.error}</h1>;
		}

		return <h1>😎</h1>;
	})()}
</div>

<For/>

React component that iterates over the of prop and renders the render prop.

import {For} from 'react-extras';

<div>
	<For of={['🌈', '🦄', '😎']} render={(item, index) =>
		<button key={index}>{item}</button>
	}/>
</div>

Or you could just use plain JavaScript:

<div>
	{['🌈', '🦄', '😎'].map((item, index) =>
		<button key={index}>{item}</button>
	)}
</div>

<Image/>

React component that improves the <img> element.

It makes the image invisible if it fails to load instead of showing the default broken image icon. Optionally, specify a fallback image URL.

import {Image} from 'react-extras';

<Image
	url="https://sindresorhus.com/unicorn.jpg"
	fallbackUrl="https://sindresorhus.com/rainbow.jpg"
/>

It supports all the props that <img> supports, but you use the prop url instead of src.

<RootClass/>

Renderless React component that can add and remove classes to the root <html> element. It accepts an add prop for adding classes, and a remove prop for removing classes. Both accept either a single class or multiple classes separated by space.

import {RootClass} from 'react-extras';

<If condition={props.isDarkMode}>
	<RootClass add="dark-mode"/>
</If>
import {RootClass} from 'react-extras';

<RootClass add="logged-in paid-user" remove="promo"/>

<BodyClass/>

Same as <RootClass/> but for <body>.

Prefer <RootClass/> though, because it's nicer to put global classes on <html> as you can consistently prefix everything with the class:

.dark-mode body {
	background: #000;
}

.dark-mode a {
	…
}

With <BodyClass/> you need to do:

body.dark-mode {
	background: #000;
}

.dark-mode a {
	…
}

isStatelessComponent(Component)

Returns a boolean of whether the given Component is a functional stateless component.

getDisplayName(Component)

Returns the display name of the given Component.

canUseDOM

A boolean of whether you're running in a context with a DOM. Can be used to check if your component is running in the browser or if it's being server-rendered.

Related