You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
This repository was archived by the owner on Jan 22, 2020. It is now read-only.
# In your express app, react-engine needs to be installed along
14
+
# side react and optionally react-router (+ history, react-router's dependency)
15
+
npm install react-engine react react-router history --save
15
16
```
16
17
17
18
### Usage On Server Side
@@ -75,13 +76,13 @@ The options object can contain properties from [react router's create configurat
75
76
76
77
Additionally, it can contain the following **optional** properties,
77
78
78
-
-`docType`: _String_ - a string that can be used as a doctype (_Default: `<!DOCTYPE html>`_).
79
-
docType might not make sense if you are rendering partials/sub page components, in that case, you should pass an empty string as docType.
80
-
-`routesFilePath`: _String_ - path for the file that contains the react router routes.
79
+
-`docType`: \<String> - a string that can be used as a doctype (_Default: `<!DOCTYPE html>`_).
80
+
(docType might not make sense if you are rendering partials/sub page components, in that case you can pass an empty string as docType)
81
+
-`routesFilePath`: \<String> - path for the file that contains the react router routes.
81
82
react-engine uses this behind the scenes to reload the routes file in
82
83
cases where [express's app property](http://expressjs.com/api.html#app.set)`view cache` is false, this way you don't need to restart the server every time a change is made in the view files or routes file.
83
-
-`renderOptionsKeysToFilter`: _Array_ - an array of keys that need to be filtered out from the data object that gets fed into the react component for rendering. [more info](#data-for-component-rendering)
84
-
-`performanceCollector`: _Function_ - to collects [perf stats](#performance-profiling)
84
+
-`renderOptionsKeysToFilter`: \<Array> - an array of keys that need to be filtered out from the data object that gets fed into the react component for rendering. [more info](#data-for-component-rendering)
85
+
-`performanceCollector`: \<Function> - to collects [perf stats](#performance-profiling)
85
86
86
87
###### Rendering views on server side
87
88
```js
@@ -150,6 +151,43 @@ Note: By default, the following three keys are always filtered out from `renderO
150
151
- `enrouten`
151
152
- `_locals`
152
153
154
+
### Handling redirects and route not found errors on the server side
155
+
While using react-router, it matches the url to a component based on the app's defined routes. react-engine captures the redirects and not-found cases that are encountered while trying to run the react-router's [match function on the server side](https://github.com/rackt/react-router/blob/5590516ec228765cbb176c81fb15fe1d4662e475/docs/guides/advanced/ServerRendering.md).
156
+
157
+
To handle the above during the lifecycle of a request, add an error type check in your express error middleware. The following are the three types of error that get thrown by react-engine:
var engine =require('react-engine').server.create({
177
-
reactRoutes:'./routes.jsx'
215
+
routes:'./routes.jsx'
178
216
performanceCollector: collector
179
217
});
180
218
```
@@ -185,6 +223,10 @@ var engine = require('react-engine').server.create({
185
223
* You can use `js` as the engine if you decide not to write your react views in `jsx`.
186
224
* [Blog on react-engine](https://www.paypal-engineering.com/2015/04/27/isomorphic-react-apps-with-react-engine/)
187
225
226
+
### Migration from 2.x to 3.x
227
+
While upgrading to 3.x version of react-engine, make sure to follow the [react-router's 1.x upgrade guide](https://github.com/rackt/react-router/blob/5590516ec228765cbb176c81fb15fe1d4662e475/upgrade-guides/v1.0.0.md) to upgrade react-router related code in your app.
228
+
Then, add to your express error middleware, react-engine's MATCH_REDIRECT and MATCH_NOT_FOUND checks.
229
+
188
230
### Migration from 1.x to 2.x
189
231
2.x version of react-engine brought in a major api change. Basically it affects the property names of the [object that gets passed in during the engine creation](https://github.com/paypal/react-engine#server-options-spec) on the server side and also how routes definition is passed into react-engine.
{/*Component that renders the active child route handler of a parent route handler component.*/}
103
-
<Router.RouteHandler {...this.props} />
106
+
{/*Router now automatically populates this.props.children of your components based on the active route. https://github.com/rackt/react-router/blob/latest/CHANGES.md#routehandler*/}
107
+
{this.props.children}
104
108
</div>
109
+
<script src='/bundle.js'></script>
105
110
</body>
106
111
</html>
107
112
);
108
113
}
109
-
});
114
+
});
110
115
111
116
// public/views/list.jsx file contains the catalog view elements of our app.
112
117
// we iterate through the array of movies and display them on this page.
@@ -119,13 +124,14 @@ $ open http://localhost:3000
119
124
<ul>
120
125
{this.props.movies.map(function(movie) {
121
126
return (
122
-
<li>
123
-
<Router.Link to='detail' params={{id:movie.id}}>
127
+
<li key={movie.id}>
128
+
<Router.Link to={'/movie/'+movie.id}>
124
129
<img src={movie.image} alt={movie.title} />
125
130
</Router.Link>
126
131
</li>
127
132
);
128
133
})}
134
+
129
135
</ul>
130
136
</div>
131
137
);
@@ -135,12 +141,12 @@ $ open http://localhost:3000
135
141
// public/views/detail.jsx file contains the markup to
136
142
// display the detail information of a movie
137
143
module.exports=React.createClass({
138
-
mixins: [Router.State],
139
144
render:functionrender() {
140
-
var movieId =this.getParams().id;
145
+
var movieId =this.props.params.id;
141
146
var movie =this.props.movies.filter(function(_movie) {
{/* Component that renders the active child route handler of a parent route handler component. */}
35
-
<Router.RouteHandler{...this.props}/>
33
+
{/* Router now automatically populates this.props.children of your components based on the active route. https://github.com/rackt/react-router/blob/latest/CHANGES.md#routehandler */}
0 commit comments