Skip to content

Commit 76e0da3

Browse files
committed
Docs
1 parent 3b1d60d commit 76e0da3

File tree

1 file changed

+58
-169
lines changed

1 file changed

+58
-169
lines changed

README.md

Lines changed: 58 additions & 169 deletions
Original file line numberDiff line numberDiff line change
@@ -8,41 +8,25 @@ Server-side rendering, client-side mounting, JSX translation, and component bund
88
```python
99
from react.render import render_component
1010

11-
component = render_component(
12-
# A path to a file exporting your React component
11+
rendered = render_component(
1312
'/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
1813
props={
1914
'foo': 'bar',
2015
'woz': [1,2,3],
2116
}
2217
)
2318

2419
# 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)
3021
```
3122

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-
3923
Documentation
4024
-------------
4125

4226
- [Installation](#installation)
27+
- [Basic usage](#basic-usage)
4328
- [API](#api)
44-
- [render_component()](#render_component)
45-
- [bundle_component()](#bundle_component)
29+
- [render_component](#render_component)
4630
- [RenderedComponent](#renderedcomponent)
4731
- [Django integration](#django-integration)
4832
- [Settings](#settings)
@@ -52,43 +36,39 @@ Documentation
5236
Installation
5337
------------
5438

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+
```
5842

59-
Install [python-webpack](https://github.com/markfinger/python-webpack)
6043

61-
Install python-react's JS dependencies
44+
Basic usage
45+
-----------
6246

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.
6648

67-
Add react-render to the functions definition of your `host.config.js` file
49+
To start the server, run
6850

69-
```javascript
70-
var reactRender = require('react-render');
51+
**TODO: once the example's settled**
7152

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
7954

80-
And install python-react
55+
```python
56+
from react.render import render_component
8157

82-
```bash
83-
pip install react
58+
rendered = render_component('path/to/component.jsx')
59+
60+
print(rendered)
8461
```
8562

63+
The object returned can be passed directly into your template layer.
64+
65+
8666

8767
API
8868
---
8969

9070

91-
### render_component()
71+
### render_component
9272

9373
Renders a component to its initial HTML. You can use this method to generate HTML on the server
9474
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
10484
from react.render import render_component
10585

10686
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
10888
path='...',
89+
10990
# An optional dictionary of data that will be passed to the renderer
11091
# and can be reused on the client-side
11192
props = {
11293
'foo': 'bar'
11394
},
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
12297
# should be used, rather than `renderToString`
12398
to_static_markup = False,
99+
124100
# An optional class which is used to encode the props to JSON
125101
json_encoder=None,
126102
)
127103
```
128104

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.
140107

141108

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-
163109
### RenderedComponent
164110

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
177112

178-
# Render JS which will mount the component over the rendered markup.
179-
# This enables you to provide immediate interactivity
180-
component.render_js()
181113
```
114+
rendered = render_component(
115+
'path/to/component.jsx',
116+
props={
117+
# ...
118+
},
119+
)
182120
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
190123
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
194127
195-
# The bundled component (a WebpackBundle instance)
196-
component.bundle
128+
# Equivalent to `rendered.markup`
129+
str(rendered)
130+
unicode(rendered)
131+
```
197132

198-
# Render a script element pointing to the bundled component
199-
print(component.bundle.render())
200133

201-
# The global variable that the bundle will exposes the component as
202-
print(component.get_var())
134+
Settings
135+
--------
203136

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
208139

209-
# The rendered markup without the container element wrapping it
210-
print(component.markup)
140+
```python
141+
from react.conf import settings
211142

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+
)
214146
```
215147

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-
223148
If you are using python-react in a Django project, add `'react'` to your `INSTALLED_APPS`
224149

225150
```python
@@ -231,59 +156,23 @@ INSTALLED_APPS = (
231156

232157
To configure python-react, place a dictionary named `REACT` into your settings file. For example
233158

234-
```python
235159
REACT = {
236-
'DEVTOOL': 'eval' if DEBUG else None,
160+
'RENDER_URL': 'http://127.0.0.1:8001/render'
237161
}
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.
269162

270-
Default: `None`
271163

272-
### TRANSLATE_TEST
164+
### RENDER_URL
273165

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.
276167

277-
Default: `'/.jsx?$/'`
168+
Default: `'http://127.0.0.1:9009`
278169

279170

280171
Running the tests
281172
-----------------
282173

283174
```bash
284175
pip install -r requirements.txt
285-
cd tests
286176
npm install
287-
cd ..
288177
python runtests.py
289178
```

0 commit comments

Comments
 (0)