Skip to content

Commit eb928d7

Browse files
authored
Merge pull request mic159#12 from mic159/react_16
React 16
2 parents 6a35d0d + 6089ae6 commit eb928d7

25 files changed

+188
-149
lines changed

.travis.yml

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2,21 +2,19 @@ language: python
22

33
python:
44
- "2.7"
5-
- "3.4"
5+
- "3.5"
6+
- "3.6"
67

78
env:
8-
- DJANGO_VERSION=1.6.10
9-
- DJANGO_VERSION=1.7.6
10-
- DJANGO_VERSION=1.8a1
11-
12-
matrix:
13-
exclude:
14-
- python: "3.4"
15-
env: DJANGO_VERSION=1.6.10
9+
- DJANGO_VERSION=1.8.18
10+
- DJANGO_VERSION=1.11.6
1611

1712
install:
1813
- "npm install -g npm"
1914
- "pip install Django==$DJANGO_VERSION"
2015
- "pip install ."
16+
- "npm install"
17+
- "npm install --prefix=tests"
18+
- "npm run build --prefix=tests"
2119

2220
script: python runtests.py

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -126,6 +126,6 @@ cd tests
126126
npm install
127127
npm run build
128128
cd ..
129-
node renderer.js &
129+
node render.js &
130130
python runtests.py
131131
```

example/djangosite/settings.py

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,6 @@
77

88
DEBUG = True
99

10-
TEMPLATE_DEBUG = DEBUG
11-
1210
SECRET_KEY = '_'
1311

1412
INSTALLED_APPS = (
@@ -28,6 +26,17 @@
2826

2927
STATIC_ROOT = os.path.join(BASE_DIR, 'static')
3028

29+
TEMPLATES = [{
30+
'BACKEND': 'django.template.backends.django.DjangoTemplates',
31+
'DIRS': [os.path.join(BASE_DIR, 'templates')],
32+
'APP_DIRS': True,
33+
'OPTIONS': {
34+
'context_processors': [
35+
'django.template.context_processors.debug',
36+
'django.template.context_processors.request',
37+
],
38+
},
39+
}]
3140

3241
# EXAMPLE APP
3342
# ===========

example/djangosite/urls.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
from django.conf.urls import patterns, include, url
1+
from django.conf.urls import include, url
22

3-
urlpatterns = patterns('',
3+
urlpatterns = [
44
url(r'^', include('example_app.urls')),
5-
)
5+
]
Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
1-
var React = require('react');
2-
var marked = require('marked');
1+
import React, {Component} from 'react';
2+
import marked from 'marked';
33

4-
module.exports = React.createClass({
5-
render: function() {
6-
var rawMarkup = marked(this.props.text);
4+
5+
export default class Comment extends Component {
6+
render() {
7+
const rawMarkup = marked(this.props.text);
78
return (
89
<div>
910
<h3>
@@ -13,4 +14,4 @@ module.exports = React.createClass({
1314
</div>
1415
);
1516
}
16-
});
17+
}
Lines changed: 30 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,25 @@
1-
var React = require('react');
2-
var $ = require('jquery');
3-
var CommentList = require('./CommentList.jsx');
4-
var CommentForm = require('./CommentForm.jsx');
1+
import React, {Component} from 'react';
2+
import $ from 'jquery';
3+
import CommentList from './CommentList.jsx';
4+
import CommentForm from './CommentForm.jsx';
55

6-
module.exports = React.createClass({
7-
getInitialState: function() {
8-
return {comments: this.props.comments};
9-
},
10-
handleCommentSubmit: function(comment) {
6+
export default class CommentBox extends Component {
7+
constructor(props) {
8+
super(props);
9+
this.state = {
10+
comments: props.comments,
11+
};
12+
}
13+
14+
handleCommentSubmit = comment => {
1115
var comments = this.state.comments;
1216
comments.push(comment);
1317
this.setState({comments: comments}, function() {
1418
this.postComment(comment);
1519
});
16-
},
17-
postComment: function(comment) {
20+
};
21+
22+
postComment = comment => {
1823
$.ajax({
1924
url: this.props.url,
2025
type: 'POST',
@@ -27,8 +32,9 @@ module.exports = React.createClass({
2732
console.error(this.props.url, status, err.toString());
2833
}.bind(this)
2934
});
30-
},
31-
getComments: function() {
35+
};
36+
37+
getComments = () => {
3238
$.ajax({
3339
url: this.props.url,
3440
dataType: 'json',
@@ -38,21 +44,24 @@ module.exports = React.createClass({
3844
error: function(xhr, status, err) {
3945
console.error(this.props.url, status, err.toString());
4046
}.bind(this),
41-
complete: this.pollForNewComments
47+
complete: this.pollForNewComments.bind(this)
4248
});
43-
},
44-
pollForNewComments: function() {
49+
};
50+
51+
pollForNewComments() {
4552
setTimeout(this.getComments, this.props.pollInterval);
46-
},
47-
componentDidMount: function() {
53+
}
54+
55+
componentDidMount() {
4856
this.pollForNewComments();
49-
},
50-
render: function() {
57+
}
58+
59+
render() {
5160
return (
5261
<div>
5362
<CommentList comments={this.state.comments} />
5463
<CommentForm url={this.props.url} onCommentSubmit={this.handleCommentSubmit} />
5564
</div>
5665
);
5766
}
58-
});
67+
}

example/example_app/static/jsx/components/CommentForm.jsx

Lines changed: 18 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,12 @@
1-
var React = require('react');
1+
import React, {Component} from 'react';
22

3-
module.exports = React.createClass({
4-
handleSubmit: function(e) {
3+
export default class CommentForm extends Component {
4+
constructor(props) {
5+
super(props);
6+
this.state = {author: '', text: ''};
7+
}
8+
9+
handleSubmit = e => {
510
e.preventDefault();
611
var author = this.state.author.trim();
712
var text = this.state.text.trim();
@@ -10,17 +15,17 @@ module.exports = React.createClass({
1015
}
1116
this.props.onCommentSubmit({author: author, text: text});
1217
this.setState({author: '', text: ''});
13-
},
14-
getInitialState: function() {
15-
return {author: '', text: ''};
16-
},
17-
handleAuthorChange: function(e) {
18+
};
19+
20+
handleAuthorChange = e => {
1821
this.setState({author: e.target.value});
19-
},
20-
handleTextChange: function(e) {
22+
};
23+
24+
handleTextChange = e => {
2125
this.setState({text: e.target.value});
22-
},
23-
render: function() {
26+
};
27+
28+
render() {
2429
return (
2530
<form method="POST" action={this.props.url} onSubmit={this.handleSubmit}>
2631
<h2>Submit a comment</h2>
@@ -45,10 +50,9 @@ module.exports = React.createClass({
4550
</label>
4651
</div>
4752
<div className="text-right">
48-
<button type="reset" className="btn btn-default">Reset</button>
4953
<button type="submit" className="btn btn-primary">Submit</button>
5054
</div>
5155
</form>
5256
);
5357
}
54-
});
58+
}

example/example_app/static/jsx/components/CommentList.jsx

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
1-
var React = require('react');
2-
var Comment = require('./Comment.jsx');
1+
import React, {Component} from 'react';
2+
import Comment from './Comment.jsx';
33

4-
module.exports = React.createClass({
5-
render: function() {
4+
5+
export default class CommentList extends Component {
6+
render() {
67
if (!this.props.comments.length) {
78
return null;
89
}
@@ -16,4 +17,4 @@ module.exports = React.createClass({
1617
</div>
1718
);
1819
}
19-
});
20+
}

example/example_app/templates/example_app/index.html

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ <h1>Django + React + Webpack demo</h1>
1616
{% if no_js %}
1717
<a href="{% url 'index' %}">View this page with JavaScript</a>
1818
{% else %}
19-
<a href="{% url 'index-no-js' %}">View this page without JavaScript</a>
19+
<a href="{% url 'index' %}?no-js">View this page without JavaScript</a>
2020
{% endif %}
2121
</p>
2222
<!-- Insert the CommentBox's rendered markup -->
@@ -35,4 +35,4 @@ <h1>Django + React + Webpack demo</h1>
3535
</script>
3636
{% endif %}
3737
</body>
38-
</html>
38+
</html>

example/example_app/urls.py

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
1-
from django.conf.urls import patterns, url
1+
from django.conf.urls import url
2+
from .views import index, comment
23

3-
urlpatterns = patterns('example_app.views',
4-
url(r'^$', 'index', name='index'),
5-
url(r'^\?no-js$', 'index', name='index-no-js'),
6-
url(r'^comment/$', 'comment', name='comment'),
7-
)
4+
urlpatterns = [
5+
url(r'^$', index, name='index'),
6+
url(r'^comment/$', comment, name='comment'),
7+
]

example/example_app/views.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ def comment(request):
4545
'text': request.POST.get('text', None),
4646
})
4747
if not request.is_ajax():
48-
return redirect('index-no-js')
48+
return redirect('/?no-js')
4949
return HttpResponse(
5050
json.dumps(comments),
5151
content_type='application/json'

example/package.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,8 @@
1212
"babel-preset-react": "^6.5.0",
1313
"jquery": "^2.1.3",
1414
"marked": "^0.3.3",
15-
"react": "^15.2",
16-
"react-dom": "^15.2",
15+
"react": "16.0",
16+
"react-dom": "16.0",
1717
"webpack": "^1.13.1",
1818
"react-render-service": "^1.0.0"
1919
}

example/requirements.txt

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1,2 @@
1-
react-render-client==1.0.0
1+
react-render-client==1.0.0
2+
django==1.11.6

package.json

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,13 +10,13 @@
1010
"body-parser": "^1.15.2",
1111
"express": "^4.14.0",
1212
"morgan": "^1.7.0",
13-
"react-dom": "^15.2",
14-
"react": "^15.2",
13+
"react-dom": "16.0.0",
14+
"react": "16.0.0",
1515
"yargs": "^3.29.0"
1616
},
1717
"peerDependencies": {
18-
"react": "^0.14 || ^15.0",
19-
"react-dom": "^0.14 || ^15.0"
18+
"react": "^15.0 || ^16.0",
19+
"react-dom": "^145.0|| ^16.0"
2020
},
2121
"author": "Michael Cooper",
2222
"repository": {

render.js

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -59,12 +59,12 @@ var Component = function Component(pathToSource) {
5959
}
6060
};
6161

62-
Component.prototype.render = function render(props, toStaticMarkup, callback) {
62+
Component.prototype.render = function render(props, toStaticMarkup) {
6363
var element = this.factory(props);
6464
if (toStaticMarkup) {
65-
callback(ReactDOMServer.renderToStaticMarkup(element));
65+
return ReactDOMServer.renderToStaticNodeStream(element);
6666
} else {
67-
callback(ReactDOMServer.renderToString(element));
67+
return ReactDOMServer.renderToNodeStream(element);
6868
}
6969
};
7070

@@ -89,15 +89,16 @@ app.post('/render', function service(request, response) {
8989
}
9090
var component = cache[pathToSource];
9191

92-
component.render(props, toStaticMarkup, function(output) {
93-
response.send(output);
94-
});
92+
var stream = component.render(props, toStaticMarkup);
93+
stream.on('error', function(err) {errorHandler(err, request, response)});
94+
stream.pipe(response);
9595
});
9696

97-
app.use(function errorHandler(err, request, response, next) {
97+
function errorHandler(err, request, response, next) {
9898
console.log('[' + new Date().toISOString() + '] ' + err.stack);
9999
response.status(500).send(argv.debug ? err.stack : err.toString());
100-
});
100+
}
101+
app.use(errorHandler);
101102

102103
var server = app.listen(argv.port || 63578, argv.host || 'localhost', function() {
103104
console.log('Started server at http://%s:%s', server.address().address, server.address().port);

runtests.py

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
import os
22
import sys
3+
import time
4+
import subprocess
35

46
import django
57

@@ -13,7 +15,14 @@
1315
from django.conf import settings
1416
from django.test.utils import get_runner
1517

18+
node = subprocess.Popen(['node', 'render.js'])
19+
# Wait for service to start
20+
time.sleep(1)
21+
1622
TestRunner = get_runner(settings)
1723
test_runner = TestRunner()
1824
failures = test_runner.run_tests(['tests'])
19-
sys.exit(bool(failures))
25+
26+
node.kill()
27+
28+
sys.exit(bool(failures))

0 commit comments

Comments
 (0)