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 Vue.js UX package

Vue.js

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

Image for the Native UX package

Native

Build native mobile apps that wrap your Symfony web application

Image for the Twig Components UX package

Twig Components

Create PHP classes that can render themselves

Browse every package

Keep React at hand.

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