Useful components and utilities for working with React
Useful components and utilities for working with React
Useful components and utilities for working with React
npm install react-extrasimport React from 'react';
import {If} from 'react-extras';
const App = props => (
);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 …;
};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';
Or you could just use plain JavaScript:
React component similar to a switch case. `` has 2 children components:
condition prop is true.that renders the children if has no with true prop condition.Note that even when the children are not rendered, they're still evaluated.
import {Choose} from 'react-extras';
Or you could just use plain JavaScript:
React component that iterates over the of prop and renders the render prop.
import {For} from 'react-extras';
Or you could just use plain JavaScript:
React component that improves the `` 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';
It supports all the props that `` supports, but you use the prop url instead of src.
Renderless React component that can add and remove classes to the root `` 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';
import {RootClass} from 'react-extras';
Same as but for.
Prefer though, because it's nicer to put global classes on as you can consistently prefix everything with the class:
.dark-mode body {
background: #000;
}
.dark-mode a {
…
}With `` you need to do:
body.dark-mode {
background: #000;
}
.dark-mode a {
…
}Inserts a separator between each element of the children.
Type: ReactNode
The elements to intersperse with separators.
Type: ReactNode | ((index: number, count: number) => ReactNode)
Default: ', '
The separator to insert between elements. Can be a React node or a function that returns a React node.
import {intersperse} from 'react-extras';
const items = ['Apple', 'Orange', 'Banana'];
const list = intersperse(
items.map(item =>
{item}
),
', '
);
// => [
Apple
, ', ',
Orange
, ', ',
Banana
]With a function separator:
import {intersperse} from 'react-extras';
const items = ['Apple', 'Orange', 'Banana'];
const list = intersperse(
items.map(item =>
{item}
),
(index, count) => index === count - 2 ? ' and ' : ', '
);
// => [
Apple
, ', ',
Orange
, ' and ',
Banana
]React component that renders the children with a separator between each element.
import {Join} from 'react-extras';
Apple
Orange
Banana
// =>
Apple
,
Orange
,
Banana
With a custom separator:
import {Join} from 'react-extras';
Home
About
Contact
// => Home | About | ContactWith a function separator:
import {Join} from 'react-extras';
index === count - 2 ? ' and ' : ', '}>
Apple
Orange
Banana
// => Apple, Orange and BananaAdds an event listener to an element and automatically removes it on cleanup.
The handler always has access to the latest props/state without needing to specify dependencies.
import {useEventListener} from 'react-extras';
function Component() {
useEventListener(document.body, 'click', event => {
console.log('Body clicked!', event);
});
return
;
}Convenience hook for useEventListener(window, …) that is SSR-safe.
import {useWindowEvent} from 'react-extras';
function Component() {
useWindowEvent('resize', event => {
console.log('Window resized!', event);
});
return
;
}Convenience hook for useEventListener(document, …) that is SSR-safe.
import {useDocumentEvent} from 'react-extras';
function Component() {
useDocumentEvent('visibilitychange', () => {
console.log('Visibility changed:', document.visibilityState);
});
return
;
}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.
No open issues yet, or sync has not completed.