Useful components and utilities for working with React
npm install react-extras
import React from 'react';
import {If} from 'react-extras';
const App = props => (
<If condition={props.showUnicorn}>
<div className="unicorn">
π¦
</div>
</If>
);
Automatically binds your React.Component
subclass methods to the instance. See the autoBind.react()
docs.
Conditionally join CSS class names together.
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',
isBlock: false,
isSmall: true
}
*/
const buttonClass = classNames(
'button',
{
[`button-${props.type}`]: Boolean(props.type),
'button-block': props.isBlock,
'button-small': props.isSmall
}
);
console.log(buttonClass);
//=> 'button button-success button-small'
return <button className={buttonClass}>β¦</button>;
};
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>
React component similar to a switch case. <Choose>
has 2 children components:
<Choose.When>
that renders the children if thecondition
prop istrue
.<Choose.Otherwise>
that renders the children if has no<Choose.When>
withtrue
propcondition
.
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>
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>
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
.
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"/>
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 {
β¦
}
Returns a boolean of whether the given Component
is a functional stateless component.
Returns the display name of the given Component
.
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.
- react-router-util - Useful components and utilities for working with React Router
- sass-extras - Useful utilities for working with Sass
- class-names - Conditionally join CSS class names together