Skip to content

React components : working twig example #778

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Apr 16, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 12 additions & 3 deletions src/React/doc/index.rst
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ Finally, to load your React components, add the following lines to ``assets/app.
// they are not necessary.
registerReactControllerComponents(require.context('./react/controllers', true, /\.(j|t)sx?$/));

That's it! Create an `assets/react/controllers/` directory and start creating your
That's it! Create an ``assets/react/controllers/`` directory and start creating your
React components.

Usage
Expand All @@ -77,6 +77,7 @@ React components located in the directory ``assets/react/controllers`` are regis
React controller components.

You can then render any React controller component in Twig using the ``react_component``.

For example:

.. code-block:: javascript
Expand All @@ -88,11 +89,19 @@ For example:
return <div>Hello {props.fullName}</div>;
}


.. code-block:: html+twig

{# templates/home.html.twig #}
<div {{ react_component('MyComponent', { 'fullName': app.user.fullName }) }}></div>
{% extends 'base.html.twig' %}

{% block body %}
<div {{ react_component('MyComponent', { 'fullName': number }) }}>
Loading... <i class="fas fa-cog fa-spin fa-3x"></i>
</div>
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
</div>
</div>
{# component living in a subdirectory: "assets/react/controllers/Admin/OtherComponent" #}
<div {{ react_component('Admin/OtherComponent') }}></div>

The name Admin/OtherComponent is correct here, right? 😅

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That's correct ! I just verified it :)


{# Component living in a subdirectory: "assets/react/controllers/Admin/OtherComponent" #}
<div {{ react_component('Admin/OtherComponent') }}></div>
{% endblock %}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm totally fine with this change - it makes it more complete, without distracting too much


Backward Compatibility promise
------------------------------
Expand Down