@@ -2,12 +2,14 @@ Symfony UX React
2
2
================
3
3
4
4
Symfony UX React is a Symfony bundle integrating `React `_ in
5
- Symfony applications. It is part of ` the Symfony UX initiative `_.
5
+ Symfony applications. It is part of the ` Symfony UX initiative `_.
6
6
7
7
React is a JavaScript library for building user interfaces.
8
8
Symfony UX React provides tools to render React components from Twig,
9
9
handling rendering and data transfers.
10
10
11
+ You can see a live example of this integration on the `Symfony UX React demo `_.
12
+
11
13
Symfony UX React supports React 18+.
12
14
13
15
Installation
@@ -41,6 +43,9 @@ React components.
41
43
Usage
42
44
-----
43
45
46
+ Register components
47
+ ~~~~~~~~~~~~~~~~~~~
48
+
44
49
The Flex recipe will have already added the ``registerReactControllerComponents() ``
45
50
code to your ``assets/app.js `` file:
46
51
@@ -55,7 +60,11 @@ This will load all React components located in the ``assets/react/controllers``
55
60
directory. These are known as **React controller components **: top-level
56
61
components that are meant to be rendered from Twig.
57
62
58
- You can render any React controller component in Twig using the ``react_component() ``.
63
+ Render in Twig
64
+ ~~~~~~~~~~~~~~
65
+
66
+ You can render any React controller component in your Twig templates, using the
67
+ ``react_component() `` function.
59
68
60
69
For example:
61
70
@@ -82,6 +91,31 @@ For example:
82
91
<div {{ react_component('Admin/OtherComponent') }}></div>
83
92
{% endblock %}
84
93
94
+ Permanent components
95
+ ~~~~~~~~~~~~~~~~~~~~
96
+
97
+ .. versionadded :: 2.21
98
+
99
+ The ability to mark a component ``permanent `` was added in UX React 2.21.
100
+
101
+ The controller responsible to render the React components can be configured
102
+ to keep the React component mounted when the root element is removed from
103
+ the DOM, using the ``permanent `` option.
104
+
105
+ This is particularly useful when the root element of a component is moved around
106
+ in the DOM or is removed and immediately re-added to the DOM (e.g. when using
107
+ `Turbo `_ and its `data-turbo-permanent ` attribute).
108
+
109
+ .. code-block :: html+twig
110
+
111
+ {# templates/home.html.twig #}
112
+ {% extends 'base.html.twig' %}
113
+
114
+ {# The React component will stay mounted if the div is moved in the DOM #}
115
+ <div {{ react_component('Hello', {fullName: 'Fabien'}, {permanent: true}) }}>
116
+ Loading...
117
+ </div>
118
+
85
119
.. _using-with-asset-mapper :
86
120
87
121
Using with AssetMapper
@@ -119,4 +153,6 @@ the Symfony framework:
119
153
https://symfony.com/doc/current/contributing/code/bc.html
120
154
121
155
.. _`React` : https://reactjs.org/
122
- .. _`the Symfony UX initiative` : https://ux.symfony.com/
156
+ .. _`Symfony UX initiative` : https://ux.symfony.com/
157
+ .. _`Symfony UX React demo` : https://ux.symfony.com/react
158
+ :: _`Turbo `: https://turbo.hotwire.dev/
0 commit comments