Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit 320bc77

Browse filesBrowse files
committed
Documentation improvements
1 parent 23473c2 commit 320bc77
Copy full SHA for 320bc77

1 file changed

+72-192Lines changed: 72 additions & 192 deletions

File tree

Expand file treeCollapse file tree
Open diff view settings
Filter options
Expand file treeCollapse file tree
Open diff view settings
Collapse file

‎README.md‎

Copy file name to clipboardExpand all lines: README.md
+72-192Lines changed: 72 additions & 192 deletions
  • Display the source diff
  • Display the rich diff
Original file line numberDiff line numberDiff line change
@@ -5,247 +5,127 @@ Django React
55

66
Render React components from a Django application.
77

8-
**Please note** that the documentation below is for the latest stable release from PyPI. Master branch is under active development and currently provides a significantly different API and workflow.
9-
10-
Documentation
11-
-------------
12-
13-
- [Basic usage](#basic-usage)
14-
- [Installation](#installation)
15-
- [ReactComponent](#reactcomponent)
16-
- [render_to_string()](#reactcomponentrender_to_string)
17-
- [render_to_static_markup()](#reactcomponentrender_to_static_markup)
18-
- [render_js()](#reactcomponentrender_js)
19-
- [render_container()](#reactcomponentrender_container)
20-
- [render_props()](#reactcomponentrender_props)
21-
- [render_source()](#reactcomponentrender_source)
22-
- [render_init()](#reactcomponentrender_init)
23-
- [ReactBundle](#reactbundle)
24-
- [render_component()](#render_component)
25-
- [Running the tests](#running-the-tests)
26-
27-
Basic usage
28-
-----------
29-
30-
Define your component
31-
32-
```python
33-
from django_react import ReactComponent
34-
35-
class MyComponent(ReactComponent):
36-
source = 'path/to/file.jsx'
37-
```
38-
39-
Create an instance of your component and pass it some props
40-
41-
```python
42-
my_component = MyComponent(
43-
some_prop='foo',
44-
some_other_prop=[1, 2, 3]
45-
)
468
```
9+
from django_react.render import render_component
4710
48-
Render the component to its initial HTML
11+
props = {
12+
'foo: 'bar',
13+
'woz': [1,2,3],
14+
}
4915
50-
```html
51-
{{ my_component.render_to_string }}
52-
```
53-
54-
Render JavaScript to mount your component
55-
56-
```html
57-
<script src="path/to/react.js"></script>
16+
rendered = render_component('path/to/component.jsx', props=props)
5817
59-
{{ my_component.render_js }}
18+
print(rendered)
6019
```
6120

62-
The rendered JavaScript will automatically include:
63-
- Your props, serialized to JSON
64-
- Your source, which will have been JSX transformed and bundled with Webpack
65-
- Initialization code that immediately mounts your component with React
21+
Documentation
22+
-------------
6623

67-
Rendering the component to its initial markup ensures that your user will see
68-
your content immediately and React will mount the component as soon as the
69-
page has loaded the JavaScript.
24+
- [Installation](#installation)
25+
- [render_component()](#render_component)
26+
- [RenderedComponent](#renderedcomponent)
27+
- [Running the tests](#running-the-tests)
7028

7129
Installation
7230
------------
7331

32+
**Note** that django-react is currently under active development and the latest
33+
stable version from PyPI is functional but far, far slower. In practice, you
34+
are recommended to use a recent version from master branch. django-react depends
35+
on django-node, which is in a similar position of active development, so you
36+
will also need to use its master branch as well.
37+
7438
```bash
75-
pip install django-react
39+
pip install -e git+https://github.com/markfinger/django-node.git#egg=django-node
40+
pip install -e git+https://github.com/markfinger/django-react.git#egg=django-react
7641
```
7742

78-
Add `'django_react'` to your `INSTALLED_APPS` setting
43+
Add django-node and django-react to your `INSTALLED_APPS` setting
7944

8045
```python
8146
INSTALLED_APPS = (
8247
# ...
48+
'django_node',
8349
'django_react',
8450
)
8551
```
8652

87-
ReactComponent
88-
--------------
89-
90-
Integrates server-side rendering of React components with Webpack's bundling.
53+
Configure django-node to host django-react's renderer.
9154

9255
```python
93-
from django_react import ReactComponent
94-
95-
# Define your component
96-
class MyComponent(ReactComponent):
97-
source = 'path/to/file.jsx'
98-
99-
# Create an instance of your component with some props
100-
component = MyComponent(
101-
foo=1,
102-
bar=[1,2,3]
56+
DJANGO_NODE = {
57+
'SERVICES': (
58+
'django_react.services',
59+
),
10360
)
104-
105-
# Render the component to its initial HTML
106-
markup = component.render_to_string()
107-
108-
# Render JS to mount the component with React
109-
js = component.render_js()
110-
```
111-
112-
### ReactComponent.render_to_string()
113-
114-
Render a component to its initial HTML. You can use this method to generate HTML
115-
on the server and send the markup down on the initial request for faster page loads
116-
and to allow search engines to crawl your pages for SEO purposes.
117-
118-
`render_to_string` takes an optional argument `wrap` which, if set to False, will
119-
prevent the the rendered output from being wrapped in the container element
120-
generated by the component's `render_container` method.
121-
122-
```html
123-
{{ component.render_to_string }}
124-
```
125-
126-
### ReactComponent.render_to_static_markup()
127-
128-
Similar to `ReactComponent.render_to_string`, except this doesn't create
129-
extra DOM attributes such as `data-react-id`, that React uses internally.
130-
This is useful if you want to use React as a simple static page generator,
131-
as stripping away the extra attributes can save lots of bytes.
132-
133-
```html
134-
{{ component.render_to_static_markup }}
13561
```
13662

137-
### ReactComponent.render_js()
63+
Start the node server which hosts the renderer.
13864

139-
Renders the script elements containing the component's props, source and
140-
initialisation. The script elements enable the browser to download your JS
141-
assets and add interactivity to a rendered component.
142-
143-
`render_js` is effectively shorthand for calling each of the component's
144-
`render_props`, `render_source`, and `render_init` methods.
145-
146-
```html
147-
{{ component.render_js }}
148-
```
149-
150-
### ReactComponent.render_container()
151-
152-
Renders a HTML element which Django React uses on the client-side to
153-
mount components with React. By default, `render_to_string` and `render_to_static_markup`
154-
are wrapped in this element.
155-
156-
`render_container` takes an optional argument, `content` which should be a string to be
157-
inserted within the element.
158-
159-
```html
160-
{{ component.render_container }}
161-
```
162-
163-
### ReactComponent.render_props()
164-
165-
Render the component's props as a JavaScript object.
166-
167-
The props will be defined within the browser's global scope under a
168-
variable name generated by the component's `get_props_variable` method.
169-
170-
```
171-
{{ component.render_props }}
172-
```
173-
174-
### ReactComponent.render_source()
175-
176-
Render a script element pointing to the bundled source of the component.
177-
178-
The bundled component will be defined within the browser's global scope
179-
under a variable name generated by the component's `get_variable` method.
180-
181-
```
182-
{{ component.render_source }}
183-
```
184-
185-
### ReactComponent.render_init()
186-
187-
Render a script element which will mount the component to the container
188-
created with the component's `render_container`, `render_to_string`,
189-
or `render_to_static_markup` methods.
190-
191-
```
192-
{{ component.render_init }}
65+
```bash
66+
./manage.py start_node_server
19367
```
19468

195-
ReactBundle
196-
-----------
197-
198-
A `django_webpack.WebpackBundle` which is configured to support loading of JSX files.
199-
200-
By default, a `ReactBundle` is configured to omit React from the generated bundle
201-
and instead rely on a global variable, `window.React`. By omitting React from the bundle,
202-
multiple components can be included in a single page without duplicating React's code.
203-
204-
You can extend the Webpack configuration by inheriting from ReactBundle and assigning
205-
the subclass as the `bundle` attribute on your ReactComponent subclass.
206-
207-
```python
208-
from django_react import ReactBundle, ReactComponent
209-
210-
class JqueryOptimisedBundle(ReactBundle):
211-
no_parse = ('jquery',)
69+
**Note** You *can* omit the step of starting the server manually,
70+
as the python process will start it as a subprocess if it is not
71+
already running. In general though, you are strongly recommended
72+
to run it as an external process as the performance will be greatly
73+
improved.
21274

213-
class MyComponent(ReactComponent):
214-
bundle = JqueryOptimisedBundle
215-
```
21675

21776
render_component()
21877
------------------
21978

220-
Render a component to its initial HTML. You can use this method to generate HTML
79+
Renders a component to its initial HTML. You can use this method to generate HTML
22180
on the server and send the markup down on the initial request for faster page loads
22281
and to allow search engines to crawl your pages for SEO purposes.
22382

83+
Returns a `RenderedComponent` instance, which can be passed directly into templates
84+
to output the component's HTML.
85+
22486
Arguments:
22587

226-
- `path_to_source`: an absolute path to a JS or JSX file which exports the component.
227-
- `serialized_props`: [optional] a string containing the JSON-serialized props which will
228-
be passed to the component.
229-
- `to_static_markup`: [optional] a boolean indicating that React's `render_to_static_markup`
88+
- `path_to_source`: an path to a JS or JSX file which exports the component. If the
89+
path is relative, django's static file finders will be used to find the file.
90+
- `props`: *optional* a dictonary that will be serialised to JSON and passed in
91+
to the component as props during the renderering process.
92+
- `to_static_markup`: *optional* a boolean indicating that React's `render_to_static_markup`
23093
method should be used for the rendering. Set this to True if you are rendering the component
23194
to static HTML and React will not be used on the client-side.
95+
- `watch_source`: *optional* a boolean indicating that the renderer should watch your source
96+
files and rebuild the component everytime it changes. Defaults to `True`, in development.
97+
- `json_encoder`: *optional* a class which is used to encode the JSON which is sent to the
98+
renderer. Defaults to `django.core.serializers.json.DjangoJSONEncoder`.
99+
100+
101+
RenderedComponent
102+
-----------------
103+
104+
The result of rendering a component to its initial HTML. RenderedComponents can be passed
105+
directly into templates where they output the generated HTML.
232106

233107
```python
234-
import json
235-
from django.contrib.staticfiles import finders
236-
from django_react import render_component
108+
# Render the component
109+
my_component = render_component(...)
237110

238-
path_to_source = finders.find('path/to/component.jsx')
111+
# Print the generated HTML
112+
print(my_component)
113+
```
114+
```html
115+
<!-- Insert the generated HTML into your template -->
116+
{{ my_component }}
117+
```
239118

240-
serialized_props = json.dumps({
241-
'foo': 1,
242-
'bar': [1, 2, 3],
243-
})
119+
RenderedComponents have a helper method, `render_props`, which outputs your JSON-serialized
120+
props. This allows you to reuse the encoded form of your props on the client-side.
244121

245-
# Render the component to its initial HTML
246-
html = render_component(path_to_source, serialized_props)
122+
```html
123+
<script>
124+
var myProps = {{ my_component.render_props }};
125+
</script>
247126
```
248127

128+
249129
Running the tests
250130
-----------------
251131

0 commit comments

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.