React Component Rendering

Render React from inside Twig

Built a React component? Render it quickly & easily in Twig and pass in dynamic props.

composer require symfony/ux-react

What React gives you

  • React in Twig

    Render a component with one Twig function.

  • Props from PHP

    Pass arrays straight into the component.

  • Server rendering

    Ship HTML first, hydrate on the client.

  • AssetMapper ready

    No bundler required to get started.

  • Island by island

    Add React only where the page needs it.

Render React components from Twig

Add React to your app.

Quickly render <React /> components & pass them props.

import React, { Component } from 'react';
import PackageList from '../components/PackageList.js';

export default class extends Component {
    constructor() {
      super();

      this.state = {
        search: ''
      }
    }

    render() {
        return (
            <div>
                <input
                    type="search"
                    placeholder="This search is built in React!"
                    className="w-full px-3 py-1.5 border border-solid border-border rounded-sm bg-body text-body-text focus:border-link focus:[outline:revert]"
                    value={this.state.search}
                    onChange={(event) => this.setState({search: event.target.value})}
                />

                <div className="mt-3">
                    <PackageList packages={this.filteredPackages()} />
                </div>
            </div>
        );
    }

    filteredPackages() {
        if (!this.state.search) {
            return this.props.packages;
        }

        return this.props.packages.filter((uxPackage) => {
            return uxPackage.humanName.toLowerCase().includes(this.state.search.toLowerCase())
        });
    }
}
{% extends 'base.html.twig' %}

{% block body %}
    <div {{ react_component('PackageSearch', {packages: packagesData}) }}>
        Loading...
    </div>
{% endblock %}
Symfony logo

UX React

Loading...

Islands inside your Twig

How a component lands on the page.

Symfony still renders the page. React takes over only the elements you hand it, with the props you built in PHP.

Registered by convention

Anything under the controllers directory is registered by name. No manifest to maintain.

assets/react/controllers/
└── Hello.jsx

Rendered from Twig

The function writes the data attributes the Stimulus controller reads to mount the component.

<div {{ react_component('Hello', {
    name: app.user.name,
}) }}></div>

Props built in PHP

Arrays and objects are serialised to JSON, so the component receives plain data.

export default function Hello({ name }) {
    return <div>Hello {name}!</div>;
}

Install React.

$ composer require symfony/ux-react

More from Symfony UX.

React is one package among many. They share the same Stimulus foundation and install the same way.

Image for the Live Components UX package

Live Components

Build dynamic interfaces with zero JavaScript

Image for the Turbo UX package

Turbo

Integration with Turbo for single-page-app and real-time experience

Image for the Stimulus UX package

Stimulus

Integration with Stimulus for HTML-powered controllers

Browse every package

Keep React at hand.

Documentation, the people behind it, and where to ask when something does not behave.