React Emoji Picker
emoji-picker-react is a popular, actively maintained React component library providing a fully customizable emoji picker for web applications. The current stable version is 4.18.0. The library receives regular updates, including significant breaking changes in major versions like 2.0.0 and 4.0.0 that refined its UI and customization model. Key differentiators include extensive customization options via CSS variables, native dark mode support, multi-language capabilities, support for custom image-based emojis, and a responsive, zero-configuration setup with various emoji styles (Apple, Google, etc.). It aims to be an out-of-the-box solution with sensible defaults while offering deep control for specific use cases.
Common errors
-
TypeError: (0, _emojiPickerReact.default) is not a function OR Error: Element type is invalid: expected a string (for built-in components) or a class/function (for composite components) but got: object.
cause Incorrect import statement for the EmojiPicker component, typically trying to use a named import for a default export, or a CommonJS require in an ESM context.fixEnsure you are using the default import: `import EmojiPicker from 'emoji-picker-react';` -
Module not found: Can't resolve 'emoji-picker-react'
cause The package is not installed or the package name is misspelled.fixInstall the package: `npm install emoji-picker-react` or `yarn add emoji-picker-react`. -
Peer dependency 'react@>=16' not met.
cause Your project's React version does not meet the minimum requirement, or React is not installed.fixEnsure your project has `react` and `react-dom` installed and updated to version 16 or higher. E.g., `npm install react react-dom` or `yarn add react react-dom`. -
Custom styling applied via props (e.g., `width`, `height`, `pickerStyle`) is not working after upgrading.
cause Major versions (v2.0.0 and v4.0.0) significantly changed the styling mechanism, removing many direct styling props in favor of SASS/CSS variables.fixFor v4.x, migrate your custom styles to use CSS variables. For v2.x-v3.x, use SASS variables. Refer to the specific major version's documentation for styling customization.
Warnings
- breaking Version 4.0.0 introduced significant breaking changes. Direct customization props were removed, and styling is now primarily handled via CSS variables. Users upgrading from v3 or earlier must refactor their styling.
- breaking Version 2.0.0 was a major redesign. It updated the UI, removed many customization props, and moved styling to SASS variables. Users upgrading from v1.x need to adjust their styling and prop usage.
- gotcha In versions prior to 2.0.2 and 1.7.2, clicking an emoji could add a '#!' to the URL, causing unexpected re-renders in applications using client-side routing like React Router.
Install
-
npm install emoji-picker-react -
yarn add emoji-picker-react -
pnpm add emoji-picker-react
Imports
- EmojiPicker
import { EmojiPicker } from 'emoji-picker-react'; const EmojiPicker = require('emoji-picker-react');import EmojiPicker from 'emoji-picker-react';
- Theme
import { Theme } from 'emoji-picker-react'; - EmojiStyle
import { EmojiStyle } from 'emoji-picker-react'; - EmojiClickData
import { EmojiClickData } from 'emoji-picker-react';
Quickstart
import React, { useState } from 'react';
import EmojiPicker, { EmojiClickData, Theme, EmojiStyle } from 'emoji-picker-react';
function App() {
const [selectedEmoji, setSelectedEmoji] = useState<string>('');
const [showPicker, setShowPicker] = useState<boolean>(false);
const onEmojiClick = (emojiObject: EmojiClickData, event: MouseEvent) => {
setSelectedEmoji(emojiObject.emoji);
setShowPicker(false); // Close picker after selection
console.log("Selected emoji data:", emojiObject);
console.log("Event:", event);
};
return (
<div style={{ display: 'flex', flexDirection: 'column', alignItems: 'center', padding: '20px' }}>
<h1>Emoji Picker React Example</h1>
<button onClick={() => setShowPicker(!showPicker)} style={{ marginBottom: '20px', padding: '10px 20px' }}>
{showPicker ? 'Hide Emoji Picker' : 'Show Emoji Picker'}
</button>
{selectedEmoji && <p style={{ fontSize: '2em' }}>You selected: {selectedEmoji}</p>}
{showPicker && (
<EmojiPicker
onEmojiClick={onEmojiClick}
theme={Theme.AUTO} // Use auto theme based on system preference
emojiStyle={EmojiStyle.APPLE} // Prefer Apple style emojis
lazyLoadEmojis={true} // Load emojis on scroll
autoFocusSearch={true}
width="100%"
height={400}
/>
)}
</div>
);
}
export default App;