How to add an emoji picker to an input field in react app

How to add an emoji picker to an input field in react app

Subscribe to my newsletter and never miss my upcoming articles

Listen to this article

Creating a new react app

npx create-react-app

Starting the app -

# npm
npm start
# yarn
yarn start

Installing the required dependencies -

# npm
npm install emoji-mart
# yarn
yarn add emoji-mart

Cleanup process

  • Delete everything inside the div in App.js and remove the import of logo.svg on top.
  • Delete logo.svg file.
  • Delete everything in App.css.
  • In index.css add this line of code -
    * {
    margin: 0;

Creating an input

import "./App.css";

function App() {
  return (
    <div className="app">
      <input type="text" placeholder="emoji picker demo" />

export default App;

Mapping to a state

import { useState } from "react";
import "./App.css";

function App() {
  const [input, setInput] = useState("");

  return (
    <div className="app">
        onChange={(e) => setInput(}
        placeholder="emoji picker demo"

export default App;

Creating an svg button for emojis

          viewBox="0 0 24 24"
            d="M14.828 14.828a4 4 0 01-5.656 0M9 10h.01M15 10h.01M21 12a9 9 0 11-18 0 9 9 0 0118 0z"

Adding some styles in App.css

.icon {
  height: 40px;
  width: 40px;

.button {
  background: transparent;
  outline: none;
  border: none;

Creating a state for showing picker

const [showEmojis, setShowEmojis] = useState(false);

Attaching it to the onClick of button

<button className="button" onClick={() => setShowEmojis(!showEmojis)}>

Rendering the emoji picker

 {showEmojis && (
          <Picker />

We will import Picker and the CSS like this

import { Picker } from "emoji-mart";
import "emoji-mart/css/emoji-mart.css";

Adding the emojis with the text in the input

Add an onSelect to the picker

<Picker onSelect={addEmoji} />

Create the addEmoji function

  const addEmoji = (e) => {
    let sym = e.unified.split("-");
    let codesArray = [];
    sym.forEach((el) => codesArray.push("0x" + el));
    let emoji = String.fromCodePoint(...codesArray);
    setInput(input + emoji);

Now our emoji picker is working! 🥳


Useful links -

Github repository

ReactJS docs

All socials

Interested in reading more such articles from Avneesh Agarwal?

Support the author by donating an amount of your choice.

Recent sponsors
Share this