How to fix emoji display issues in a React and Laravel app?

Updated: Feb 14, 2025

How to fix emoji display issues in a React and Laravel app?

To fix emoji display issues in a React and Laravel app, you need to ensure that both the client-side (React) and server-side (Laravel) are properly configured to handle emojis. Here are the steps to follow:

  1. Laravel Configuration:

    a. Install the emoji package by running the following command in your terminal:

    composer require symfony/emoji
    

    b. Import the package in your composer.json file:

    "autoload": {
        "psr-4": {
            "App\\": "app/"
        },
        "files": [
            "vendor/autoload.php"
        ]
    },
    "require": {
        "symfony/emoji": "^4.0"
    }
    

    c. Register the package service provider in your app/Providers/AppServiceProvider.php file:

    namespace App\Providers;
    
    use Illuminate\Support\ServiceProvider;
    use Symfony\Component\Emoji\Emoji;
    
    class AppServiceProvider extends ServiceProvider
    {
        /**
         * Register any application services.
         *
         * @return void
         */
        public function register()
        {
            if ($this->app->environment('local', 'testing')) {
                $this->app->singleton(Emoji::class);
            }
        }
    }
    

    d. Use the Emoji class to convert emojis to their Unicode representation in your Laravel controllers or views:

    namespace App\Http\Controllers;
    
    use Illuminate\Http\Request;
    use App\Emoji;
    
    class ExampleController extends Controller
    {
        public function index()
        {
            $emoji = new Emoji();
            $emojiString = $emoji->parse(':smile:');
            return view('example.index', ['emoji' => $emojiString]);
        }
    }
    
    @extends('layouts.app')
    
    @section('content')
        <p>{{ $emoji }}</p>
    @endsection
    
  2. React Configuration:

    a. Install the react-emoji package by running the following command in your terminal:

    npm install react-emoji
    

    b. Import the package in your React component:

    import Emoji from 'react-emoji';
    
    function Example() {
        return (
            <div>
                <p>Hello, :smile:</p>
                <p><Emoji emoji=":smile:" /></p>
            </div>
        );
    }
    

    c. If you're using Next.js, you might need to install the react-emoji package as a dev dependency and add it to your next.config.js file:

    module.exports = {
        reactStrictMode: true,
        webpack: (config, { dev, isServer }) => {
            if (!dev && !isServer) {
                config.resolve.fallback = {
                    'react-emoji': require.resolve('react-emoji'),
                };
            }
        },
    };
    

    d. Use the Emoji component to render emojis in your React components:

    import Emoji from 'react-emoji';
    
    function Example() {
        return (
            <div>
                <p>Hello, :smile:</p>
                <p><Emoji emoji=":smile:" /></p>
            </div>
        );
    }
    

With these configurations in place, your React and Laravel app should be able to display emojis correctly.