tystr/react-js-bundle

A bundle for integrating reactjs into symfony. Provides server-side rendering via the v8js php extension for isomorphic apps.

Installs: 12

Dependents: 0

Suggesters: 0

Security: 0

Stars: 13

Watchers: 3

Forks: 2

Open Issues: 0

Type:symfony-bundle

v0.2.0 2016-03-10 07:37 UTC

This package is not auto-updated.

Last update: 2024-12-17 20:40:35 UTC


README

Build Status Code Climate Test Coverage

A bundle for integrating React into Symfony. Provides server-side rendering via the v8js PHP extension for building isomorphic applications.

Installation

Install tystr/react-js-bundle with composer:

# composer.phar require tystr/react-js-bundle:dev-master:~0.1

Configuration

Register the bundle with your application:

<?php
// app/AppKernel.php

public function registerBundles()
{
    $bundles = array(
        // ...
        new Tystr\ReactJsBundle\TystrReactJsBundle(),
        // ...
    );
}

Configure the paths to your react.js and components javascript files:

# app/config.yml

tystr_react_js:
    react_path: path/to/react.js
    components_path: path/to/components.js

By default, the v8js PHP extension is used to render the react components. If you would prefer to use an external server to render the react components, you may configure an external rendering method:

# app/config.yml

tystr_react_js:
    react_path: path/to/react.js
    components_path: path/to/components.js
    render_method: external
    render_url: http://localhost:3000

This will cause a GET request to be made to the render_url value with the component and data ({name: Tyler} in this case) in the url as query parameters:

GET http://localhost:3000?component=MyComponent&data=%7B%22name%22%3A%22Tyler%22%7D

Usage

{{ react_component('MyComponent', 'my-component') }}

This will render the react component MyComponent on the server-side and place it inside a div with the id my-component.

To pass data to a component, pass a hash as the third argument:

{{ react_component('MyComponent', 'my-component', {'name': 'Tyler'}) }}

This makes this.props.name available in MyComponent.

To mount all components rendered server-side with the react_component function, use the react_mount_components twig function:

<script>
    {{ react_mount_components() }}
</script>

To mount a single react component (as long as it's already rendered with react_component), use the react_mount_component function:

<script>
    {{ react_mount_component('MyComponent') }}
</script>

Attempting to mount a component whose markup has not been rendered will result in an exception Tystr\ReactJsBundle\Exception\ComponentNotRenderedException.