@@ -8,41 +8,25 @@ Server-side rendering, client-side mounting, JSX translation, and component bund
8
8
``` python
9
9
from react.render import render_component
10
10
11
- component = render_component(
12
- # A path to a file exporting your React component
11
+ rendered = render_component(
13
12
' /path/to/component.jsx' ,
14
- # Translate the source to JavaScript from JSX + ES6/7
15
- translate = True ,
16
- # Props that will be passed to the renderer and reused on
17
- # the client-side to provide immediate interactivity
18
13
props = {
19
14
' foo' : ' bar' ,
20
15
' woz' : [1 ,2 ,3 ],
21
16
}
22
17
)
23
18
24
19
# The rendered markup
25
- print (component)
26
-
27
- # Outputs JavaScript which will mount the component on the client-side and
28
- # provide immediate interactivity
29
- print (component.render_js())
20
+ print (rendered)
30
21
```
31
22
32
- [ render_component] ( #render_component ) is the main entry point to pre-render components and package them for
33
- use on the client-side.
34
-
35
- If you only want to use your JSX files on the client-side, you can use [ bundle_component] ( #bundle_component )
36
- to translate and bundle the source code into a form that will run in a browser.
37
-
38
-
39
23
Documentation
40
24
-------------
41
25
42
26
- [ Installation] ( #installation )
27
+ - [ Basic usage] ( #basic-usage )
43
28
- [ API] ( #api )
44
- - [ render_component()] ( #render_component )
45
- - [ bundle_component()] ( #bundle_component )
29
+ - [ render_component] ( #render_component )
46
30
- [ RenderedComponent] ( #renderedcomponent )
47
31
- [ Django integration] ( #django-integration )
48
32
- [ Settings] ( #settings )
@@ -52,43 +36,39 @@ Documentation
52
36
Installation
53
37
------------
54
38
55
- python-react depends on [ js-host ] ( https://github.com/markfinger/python-js-host/ ) to provide
56
- interoperability with JavaScript. Complete its
57
- [ quick start ] ( https://github.com/markfinger/python-js-host/#quick-start ) before continuing.
39
+ ``` bash
40
+ pip install react
41
+ ```
58
42
59
- Install [ python-webpack] ( https://github.com/markfinger/python-webpack )
60
43
61
- Install python-react's JS dependencies
44
+ Basic usage
45
+ -----------
62
46
63
- ``` bash
64
- npm install --save react react-render babel-core babel-loader
65
- ```
47
+ python-react provides a high-level interface to a server which is capable of rendering React components.
66
48
67
- Add react-render to the functions definition of your ` host.config.js ` file
49
+ To start the server, run
68
50
69
- ``` javascript
70
- var reactRender = require (' react-render' );
51
+ ** TODO: once the example's settled**
71
52
72
- module .exports = {
73
- functions: {
74
- // ...
75
- react: reactRender
76
- }
77
- };
78
- ```
53
+ Render requests should provide a path to a JS file that exports a React component
79
54
80
- And install python-react
55
+ ``` python
56
+ from react.render import render_component
81
57
82
- ``` bash
83
- pip install react
58
+ rendered = render_component(' path/to/component.jsx' )
59
+
60
+ print (rendered)
84
61
```
85
62
63
+ The object returned can be passed directly into your template layer.
64
+
65
+
86
66
87
67
API
88
68
---
89
69
90
70
91
- ### render_component()
71
+ ### render_component
92
72
93
73
Renders a component to its initial HTML. You can use this method to generate HTML on the server
94
74
and send the markup down on the initial request for faster page loads and to allow search engines
@@ -104,122 +84,67 @@ front end to output the component's markup and to mount the component for client
104
84
from react.render import render_component
105
85
106
86
render_component(
107
- # A path to a file which exports your React component
87
+ # A absolute path to a file which exports your React component
108
88
path = ' ...' ,
89
+
109
90
# An optional dictionary of data that will be passed to the renderer
110
91
# and can be reused on the client-side
111
92
props = {
112
93
' foo' : ' bar'
113
94
},
114
- # An optional boolean indicating that the component should be bundled and
115
- # translated from JSX and ES6/7 before rendering. Components are translated
116
- # with Babel
117
- translate = True ,
118
- # An optional boolean indicating that the component should be bundled for
119
- # before rendering. If `translate` is set to True, this argument is ignored
120
- bundle = True ,
121
- # An optional boolean indicating that React's `renderToStaticMarkup` method
95
+
96
+ # An optional boolean indicating that React's `renderToStaticMarkup` method
122
97
# should be used, rather than `renderToString`
123
98
to_static_markup = False ,
99
+
124
100
# An optional class which is used to encode the props to JSON
125
101
json_encoder = None ,
126
102
)
127
103
```
128
104
129
-
130
- ### bundle_component()
131
-
132
- Packages a React component so that it can be re-used on the client-side. JSX + ES6+7 files are translated
133
- to JavaScript with [ Babel] ( https://babeljs.io/ ) .
134
-
135
- Be aware that ` bundle_component ` is primarily a convenience method. Under the hood, it simply generates a webpack
136
- config file which is passed to [ python-webpack] ( https://github.com/markfinger/python-webpack ) .
137
-
138
- If you require more flexibility in the bundling process, you are recommended to read the code and then use
139
- python-webpack directly.
105
+ If you are using python-react in a Django project, relative paths to components will be resolved
106
+ via Django's static file finders.
140
107
141
108
142
- #### Usage
143
-
144
- ``` python
145
- from react.bundle import bundle_component
146
-
147
- bundle = bundle_component(
148
- # A path to a file which exports the component. If the path is relative,
149
- # django's static file finders will attempt to find the file
150
- path = ' ...' ,
151
- # An optional boolean indicating that the component should be translated
152
- # from JSX and ES6/7 during the bundling process
153
- translate = True ,
154
- )
155
-
156
- # Render a <script> element pointing to the bundle
157
- bundle.render()
158
-
159
- # Returns the variable that the bundle exposes the component as
160
- bundle.get_var()
161
- ```
162
-
163
109
### RenderedComponent
164
110
165
- The result of rendering a component to its initial markup. RenderedComponents can be converted to
166
- strings to output their generated markup. If ` translate ` or ` bundle ` was provided to ` render_component ` ,
167
- the component can also be mounted on the client-side to provide immediate interactivity.
168
-
169
- ``` python
170
- component = render_component(... )
171
-
172
- # Outputs the generated markup
173
- str (component)
174
-
175
- # Also outputs the generated markup
176
- component.render_markup()
111
+ An object representing the output from the rendering process
177
112
178
- # Render JS which will mount the component over the rendered markup.
179
- # This enables you to provide immediate interactivity
180
- component.render_js()
181
113
```
114
+ rendered = render_component(
115
+ 'path/to/component.jsx',
116
+ props={
117
+ # ...
118
+ },
119
+ )
182
120
183
- Note: if you wish to use the ` render_js ` method on the client-side, you ** must** provide a ` <script> `
184
- element pointing to React. React is omitted from the bundled component so that build times are reduced,
185
- and to ensure that multiple components can be included on a single page without duplicating React's
186
- codebase.
187
-
188
- Be aware that the mounting strategy used by ` render_js ` is only intended for convenience. If you want to
189
- use a more custom solution for mounting or bundling, there are a couple of helpers provided to assist you:
121
+ # The markup generated from rendering the component with the provided props
122
+ rendered.markup
190
123
191
- ``` python
192
- # The data used to render the component, this can be plugged straight into the client-side
193
- component.render_props()
124
+ # The value of the `props` argument serialized to JSON. You can pass this directly
125
+ # into your template layer
126
+ rendered.props
194
127
195
- # The bundled component (a WebpackBundle instance)
196
- component.bundle
128
+ # Equivalent to `rendered.markup`
129
+ str(rendered)
130
+ unicode(rendered)
131
+ ```
197
132
198
- # Render a script element pointing to the bundled component
199
- print (component.bundle.render())
200
133
201
- # The global variable that the bundle will exposes the component as
202
- print (component.get_var())
134
+ Settings
135
+ --------
203
136
204
- # When rendering a bundled component, the component is wrapped in a container
205
- # element to allow the mounting JS to target it. You can use this selector to
206
- # target the container element yourself
207
- print (component.get_container_id())
137
+ Settings can be defined by calling ` react.conf.settings.configure ` with keyword arguments matching
138
+ the setting that you want to define. For example
208
139
209
- # The rendered markup without the container element wrapping it
210
- print (component.markup)
140
+ ``` python
141
+ from react.conf import settings
211
142
212
- # Render the JS used to mount the bundled component over the rendered component
213
- print (component.render_mount_js())
143
+ settings.configure(
144
+ RENDER_URL = ' http://127.0.0.1:8001/render' ,
145
+ )
214
146
```
215
147
216
- The codebase in ` react/bundle.py ` also provides a number of examples illustrating how you can
217
- programmatically generate configuration files for webpack.
218
-
219
-
220
- Django integration
221
- ------------------
222
-
223
148
If you are using python-react in a Django project, add ` 'react' ` to your ` INSTALLED_APPS `
224
149
225
150
``` python
@@ -231,59 +156,23 @@ INSTALLED_APPS = (
231
156
232
157
To configure python-react, place a dictionary named ` REACT ` into your settings file. For example
233
158
234
- ``` python
235
159
REACT = {
236
- ' DEVTOOL ' : ' eval ' if DEBUG else None ,
160
+ 'RENDER_URL ': 'http://127.0.0.1:8001/render '
237
161
}
238
- ```
239
-
240
- When calling ` render_component ` or ` bundle_component ` , python-react will attempt to use Django's
241
- static file finders to resolve relative paths. If you provide a relative path such as
242
- ` 'my_app/component.jsx' ` , Django may resolve that path to an app's static folder, for example:
243
- ` 'my_app/static/my_app/component.jsx' ` .
244
-
245
-
246
- Settings
247
- --------
248
-
249
- Settings can be defined by calling ` react.conf.settings.configure ` with keyword arguments matching
250
- the setting that you want to define. For example
251
-
252
- ``` python
253
- from react.conf import settings
254
-
255
- DEBUG = True
256
-
257
- settings.configure(
258
- DEVTOOL = ' eval' if DEBUG else None ,
259
- )
260
- ```
261
-
262
- ### DEVTOOL
263
-
264
- The [ devtool] ( http://webpack.github.io/docs/configuration.html#devtool ) that webpack uses when
265
- bundling components.
266
-
267
- During development, you are recommended to set this to ` 'eval' ` , as it will assist with debugging
268
- translated and bundled assets.
269
162
270
- Default: ` None `
271
163
272
- ### TRANSLATE_TEST
164
+ ### RENDER_URL
273
165
274
- When bundling a component with ` translate=True ` , this JavaScript regex is tested against every file to
275
- determine if the babel loader should run over it.
166
+ A complete url to an endpoint which accepts POST requests conforming to react-render's configuration API.
276
167
277
- Default: ` '/.jsx?$/' `
168
+ Default: ` 'http://127.0.0.1:9009 `
278
169
279
170
280
171
Running the tests
281
172
-----------------
282
173
283
174
``` bash
284
175
pip install -r requirements.txt
285
- cd tests
286
176
npm install
287
- cd ..
288
177
python runtests.py
289
178
```
0 commit comments