@@ -3,31 +3,67 @@ django-react
33
44[ ![ Build Status] ( https://travis-ci.org/markfinger/django-react.svg?branch=master )] ( https://travis-ci.org/markfinger/django-react )
55
6- Render and bundle React components from a Django application .
6+ Server-side rendering, client-side mounting, JSX translation, and component bundling .
77
88``` python
99from django_react.render import render_component
1010
11- # Render a JSX component
12- component = render_component(' path/to/component.jsx' , translate = True , props = {
13- ' foo' : ' bar' ,
14- ' woz' : [1 ,2 ,3 ],
15- })
11+ component = render_component(
12+
13+ # A path to a module exporting your React component
14+ ' path/to/component.jsx' ,
15+
16+ # Translate the source to JavaScript from JSX + ES6/7
17+ translate = True ,
18+
19+ # Props that will be passed to the renderer, and will be reused
20+ # on the client-side to provide client-side interactivity
21+ props = {
22+ ' foo' : ' bar' ,
23+ ' woz' : [1 ,2 ,3 ],
24+ }
25+
26+ )
1627
1728# The rendered markup
1829print (component)
1930
20- # Render JavaScript that will reuse the data provided and mount the component
21- # on the client-side
31+ # Render JS which will mount the component on the client-side and
32+ # provide interactivity
2233print (component.render_js())
2334```
2435
36+ If you only want to pre-compile a JSX component to JS, you can bundle your component
37+ into a single file by calling ` bundle_component ` .
38+
39+ ``` python
40+ from django_react.bundle import bundle_component
41+
42+ bundle = bundle_component(
43+
44+ # A path to a module exporting your React component
45+ ' path/to/component.jsx,
46+
47+ # Translate the source to JavaScript from JSX + ES6/7
48+ translate = True
49+
50+ )
51+
52+ # Renders a script element pointing to the bundled component
53+ print (bundle.render())
54+
55+ # Outputs the variable name that the component is exposed as.
56+ print (bundle.get_library())
57+ ```
58+
59+
2560Documentation
2661-------------
2762
2863- [ Installation] ( #installation )
2964- [ render_component()] ( #render_component )
3065- [ RenderedComponent] ( #renderedcomponent )
66+ - [ bundle_component()] ( #bundle_component )
3167- [ Usage in development] ( #usage-in-development )
3268- [ Usage in production] ( #usage-in-production )
3369- [ Running the tests] ( #running-the-tests )
@@ -80,27 +116,49 @@ on the server and send the markup down on the initial request for faster page lo
80116and to allow search engines to crawl your pages for SEO purposes.
81117
82118Returns a ` RenderedComponent ` instance, which can be passed directly into templates
83- to output the component's HTML and to mount the component for client-side interactivity.
84-
85- Arguments:
86-
87- - ** path_to_source** — a path to a file which exports the component. If the path is relative,
88- django's static file finders will be used to find the file.
89- - ** props** * optional* — a dictonary that will be serialised to JSON and passed to
90- the component during the renderering process.
91- - ** to_static_markup** * optional* — a boolean indicating that React's ` renderToStaticMarkup `
92- method should be used, rather than ` renderToString ` .
93- - ** bundle** * optional* - a boolean indicating that the component should be bundled for
94- reuse on the client-side. If ` translate ` or ` watch_source ` are used, this argument is
95- ignored.
96- - ** translate** * optional* - a boolean indicating that the component should be translated
97- from JSX and ES6/7 before rendering. Components are translated with
98- [ Babel] ( http://babeljs.io ) .
99- - ** watch_source** * optional* — a boolean indicating that your source files should be watched
100- for changes. When changes are detected, the component is rebuilt in background, ready for
101- the next request. If not defined, defaults to ` django.conf.settings.DEBUG ` .
102- - ** json_encoder** * optional* — a class which is used to encode the props to JSON. Defaults
103- to ` django.core.serializers.json.DjangoJSONEncoder ` .
119+ to output the component's HTML, and to mount the component for client-side interactivity.
120+
121+ ** Configuration**
122+
123+ ``` python
124+ from django_react.render import render_component
125+
126+ render_component(
127+
128+ # A path to a file which exports the component. If the path is relative,
129+ # django's static file finders will attempt to find the file
130+ path_to_source = ' ...' ,
131+
132+ # [optional] a dictionary that will be serialised to JSON and passed to
133+ # the component during the rendering process
134+ props = {
135+ ' foo' : ' bar'
136+ },
137+
138+ # [optional] a boolean indicating that React's `renderToStaticMarkup`
139+ # method should be used, rather than `renderToString`
140+ to_static_markup = False ,
141+
142+ # [optional] a boolean indicating that the component should be bundled for
143+ # reuse on the client-side. If `translate` or `watch_source` are provided, this
144+ # argument is ignored
145+ bundle = True ,
146+
147+ # [optional] a boolean indicating that the component should be translated
148+ # from JSX and ES6/7 before rendering. Components are translated with Babel
149+ translate = True ,
150+
151+ # [optional] a boolean indicating that your source files should be watched
152+ # for changes. When changes are detected, the component is rebuilt in background,
153+ # ready for the next request. If not defined, defaults to `DEBUG`
154+ watch_source = True ,
155+
156+ # [optional] a class which is used to encode the props to JSON. Defaults
157+ # to `django.core.serializers.json.DjangoJSONEncoder`
158+ json_encoder = None ,
159+
160+ )
161+ ```
104162
105163
106164RenderedComponent
@@ -165,6 +223,43 @@ print(component.markup)
165223print (component.render_mount_js())
166224```
167225
226+
227+ bundle_component()
228+ ------------------
229+
230+ Packages a React component so that it can be re-used on the client-side. JSX + ES6+7
231+ files can translated to JavaScript with [ Babel] ( https://babeljs.io/ ) .
232+
233+ Be aware that ` bundle_component ` is a convenience method which plugs a pre-built
234+ webpack config into [ django-webpack] ( https://github.com/markfinger/django-webpack ) .
235+ If you require more flexibility in the bundling process, you are recommended to
236+ read the code to understand what is happening, and then use django-webpack yourself.
237+
238+ ** Configuration**
239+
240+ ``` python
241+ from django_react.bundle import bundle_component
242+
243+ bundle_component(
244+
245+ # A path to a file which exports the component. If the path is relative,
246+ # django's static file finders will attempt to find the file
247+ path = ' ...' ,
248+
249+ # [optional] a boolean indicating that the component should be translated
250+ # from JSX and ES6/7 during the bundling process. Components are translated
251+ # with Babel
252+ translate = True ,
253+
254+ # [optional] a boolean indicating that your source files should be watched
255+ # for changes. When changes are detected, the component is rebuilt in
256+ # background, ready for the next request. If not defined, defaults to `DEBUG`
257+ watch_source = True ,
258+
259+ )
260+ ```
261+
262+
168263Usage in development
169264--------------------
170265
0 commit comments