Baike.dev
All toolsOnline toolsAI codingTrendingOpen sourceNewsSubmit
Log in
< Back to tools
R

react-extras

> 前端框架
Open source

Useful components and utilities for working with React

744 stars0 likes1 views
WebsiteGitHub

About

Useful components and utilities for working with React

react-extras

Useful components and utilities for working with React

Install

bash
npm install react-extras

Usage

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

const App = props => (
	
		

	
);

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.

javascript
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'
javascript
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:

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

Or you could just use plain JavaScript:

javascript

``

React component similar to a switch case. `` has 2 children components:

  • `` that renders the children if the 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.

javascript
import {Choose} from 'react-extras';

Or you could just use plain JavaScript:

javascript

``

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

javascript
import {For} from 'react-extras';

Or you could just use plain JavaScript:

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.

javascript
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.

javascript
import {RootClass} from 'react-extras';

	
javascript
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:

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

.dark-mode a {
	…
}

With `` you need to do:

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

.dark-mode a {
	…
}

intersperse(children, separator?)

Inserts a separator between each element of the children.

children

Type: ReactNode

The elements to intersperse with separators.

separator

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.

javascript
import {intersperse} from 'react-extras';

const items = ['Apple', 'Orange', 'Banana'];
const list = intersperse(
	items.map(item => 
{item}
),
	', '
);
// => [
Apple
, ', ', 
Orange
, ', ', 
Banana
]

With a function separator:

javascript
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.

javascript
import {Join} from 'react-extras';

	
Apple

	
Orange

	
Banana

// => 
Apple
, 
Orange
, 
Banana

With a custom separator:

javascript
import {Join} from 'react-extras';

	Home
	About
	Contact

// => Home | About | Contact

With a function separator:

javascript
import {Join} from 'react-extras';

 index === count - 2 ? ' and ' : ', '}>
	Apple
	Orange
	Banana

// => Apple, Orange and Banana

useEventListener(target, eventName, handler, options?)

Adds 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.

javascript
import {useEventListener} from 'react-extras';

function Component() {
	useEventListener(document.body, 'click', event => {
		console.log('Body clicked!', event);
	});

	return 
;
}

useWindowEvent(eventName, handler, options?)

Convenience hook for useEventListener(window, …) that is SSR-safe.

javascript
import {useWindowEvent} from 'react-extras';

function Component() {
	useWindowEvent('resize', event => {
		console.log('Window resized!', event);
	});

	return 
;
}

useDocumentEvent(eventName, handler, options?)

Convenience hook for useEventListener(document, …) that is SSR-safe.

javascript
import {useDocumentEvent} from 'react-extras';

function Component() {
	useDocumentEvent('visibilitychange', () => {
		console.log('Visibility changed:', document.visibilityState);
	});

	return 
;
}

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

  • 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

Issues· 0 open

View all issuesOpen on GitHub

No open issues yet, or sync has not completed.

> Tags

JavaScriptnpm-packagereactreact-componentreact-utils

No comments yet. Be the first to share.

> Details

PublishedAug 1, 2026
UpdatedSep 17, 2026
Category前端框架
PricingOpen source

> Related tools

R
React
用于构建用户界面的 JavaScript 库
V
Vue.js
渐进式 JavaScript 框架
N
Next.js
基于 React 的全栈 Web 框架