Skip to content

Commit 1b2293b

Browse files
committed
[fixed] use context in examples
1 parent 69c4f67 commit 1b2293b

File tree

11 files changed

+80
-40
lines changed

11 files changed

+80
-40
lines changed

examples/animations/app.js

+7-5
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,16 @@
1-
// TODO: animations aren't happening, not sure what the problem is
21
var React = require('react');
32
var TransitionGroup = require('react/lib/ReactCSSTransitionGroup');
43
var Router = require('react-router');
54
var { Route, RouteHandler, Link } = Router;
65

76
var App = React.createClass({
8-
mixins: [ Router.State ],
7+
8+
contextTypes: {
9+
router: React.PropTypes.func.isRequired
10+
},
911

1012
render: function () {
11-
var name = this.getRoutes().slice(0).reverse()[0].name;
13+
var name = this.context.router.getCurrentRoutes().slice(0).reverse()[0].name;
1214

1315
return (
1416
<div>
@@ -48,8 +50,8 @@ var Page2 = React.createClass({
4850

4951
var routes = (
5052
<Route handler={App}>
51-
<Route name="page1" handler={Page1} addHandlerKey={true} />
52-
<Route name="page2" handler={Page2} addHandlerKey={true} />
53+
<Route name="page1" handler={Page1} />
54+
<Route name="page2" handler={Page2} />
5355
</Route>
5456
);
5557

examples/auth-flow/app.js

+10-6
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
var React = require('react');
22
var Router = require('react-router');
3-
var { Route, RouteHandler, Link, State } = Router;
3+
var { Route, RouteHandler, Link } = Router;
44

55
var App = React.createClass({
66
getInitialState: function () {
@@ -65,7 +65,10 @@ var Dashboard = React.createClass({
6565
});
6666

6767
var Login = React.createClass({
68-
mixins: [ Router.Navigation, State],
68+
69+
contextTypes: {
70+
router: React.PropTypes.func.isRequired
71+
},
6972

7073
getInitialState: function () {
7174
return {
@@ -75,17 +78,18 @@ var Login = React.createClass({
7578

7679
handleSubmit: function (event) {
7780
event.preventDefault();
78-
var nextPath = this.getQuery().nextPath;
81+
var { router } = this.context;
82+
var nextPath = router.getCurrentQuery().nextPath;
7983
var email = this.refs.email.getDOMNode().value;
8084
var pass = this.refs.pass.getDOMNode().value;
8185
auth.login(email, pass, function (loggedIn) {
8286
if (!loggedIn)
8387
return this.setState({ error: true });
8488

8589
if (nextPath) {
86-
this.replaceWith(nextPath);
90+
router.replaceWith(nextPath);
8791
} else {
88-
this.replaceWith('/about');
92+
router.replaceWith('/about');
8993
}
9094
}.bind(this));
9195
},
@@ -164,7 +168,7 @@ function pretendRequest(email, pass, cb) {
164168
if (email === '[email protected]' && pass === 'password1') {
165169
cb({
166170
authenticated: true,
167-
token: Math.random().toString(36).substring(7),
171+
token: Math.random().toString(36).substring(7)
168172
});
169173
} else {
170174
cb({authenticated: false});

examples/data-flow/app.js

+10-5
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,9 @@ var { Route, RouteHandler, Link } = Router;
44

55
var App = React.createClass({
66

7-
mixins: [ Router.Navigation ],
7+
contextTypes: {
8+
router: React.PropTypes.func.isRequired
9+
},
810

911
getInitialState: function () {
1012
return {
@@ -28,7 +30,7 @@ var App = React.createClass({
2830
return taco.name != removedTaco;
2931
});
3032
this.setState({tacos: tacos});
31-
this.transitionTo('/');
33+
this.context.router.transitionTo('/');
3234
},
3335

3436
render: function () {
@@ -54,16 +56,19 @@ var App = React.createClass({
5456
});
5557

5658
var Taco = React.createClass({
57-
mixins: [ Router.State ],
59+
60+
contextTypes: {
61+
router: React.PropTypes.func.isRequired
62+
},
5863

5964
remove: function () {
60-
this.props.onRemoveTaco(this.getParams().name);
65+
this.props.onRemoveTaco(this.context.router.getCurrentParams().name);
6166
},
6267

6368
render: function () {
6469
return (
6570
<div className="Taco">
66-
<h1>{this.getParams().name}</h1>
71+
<h1>{this.context.router.getCurrentParams().name}</h1>
6772
<button onClick={this.remove}>remove</button>
6873
</div>
6974
);

examples/dynamic-segments/app.js

+10-4
Original file line numberDiff line numberDiff line change
@@ -17,10 +17,13 @@ var App = React.createClass({
1717
});
1818

1919
var User = React.createClass({
20-
mixins: [ Router.State ],
20+
21+
contextTypes: {
22+
router: React.PropTypes.func.isRequired
23+
},
2124

2225
render () {
23-
var { userId } = this.getParams();
26+
var { userId } = this.context.router.getCurrentParams();
2427
return (
2528
<div className="User">
2629
<h1>User id: {userId}</h1>
@@ -36,10 +39,13 @@ var User = React.createClass({
3639

3740

3841
var Task = React.createClass({
39-
mixins: [ Router.State ],
42+
43+
contextTypes: {
44+
router: React.PropTypes.func.isRequired
45+
},
4046

4147
render () {
42-
var { userId, taskId } = this.getParams();
48+
var { userId, taskId } = this.context.router.getCurrentParams();
4349
return (
4450
<div className="Task">
4551
<h2>User id: {userId}</h2>

examples/master-detail/app.js

+11-6
Original file line numberDiff line numberDiff line change
@@ -68,10 +68,12 @@ var Index = React.createClass({
6868

6969
var Contact = React.createClass({
7070

71-
mixins: [ Router.Navigation, Router.State ],
71+
contextTypes: {
72+
router: React.PropTypes.func.isRequired
73+
},
7274

7375
getStateFromStore: function () {
74-
var id = this.getParams().id;
76+
var id = this.context.router.getCurrentParams().id;
7577
return {
7678
contact: ContactStore.getContact(id)
7779
};
@@ -101,9 +103,10 @@ var Contact = React.createClass({
101103
},
102104

103105
destroy: function () {
104-
var id = this.getParams().id;
106+
var { router } = this.context;
107+
var id = router.getCurrentParams().id;
105108
ContactStore.removeContact(id);
106-
this.transitionTo('/');
109+
router.transitionTo('/');
107110
},
108111

109112
render: function () {
@@ -122,15 +125,17 @@ var Contact = React.createClass({
122125

123126
var NewContact = React.createClass({
124127

125-
mixins: [ Router.Navigation ],
128+
contextTypes: {
129+
router: React.PropTypes.func.isRequired
130+
},
126131

127132
createContact: function (event) {
128133
event.preventDefault();
129134
ContactStore.addContact({
130135
first: this.refs.first.getDOMNode().value,
131136
last: this.refs.last.getDOMNode().value
132137
}, function (contact) {
133-
this.transitionTo('contact', { id: contact.id });
138+
this.context.router.transitionTo('contact', { id: contact.id });
134139
}.bind(this));
135140
},
136141

examples/query-params/app.js

+7-3
Original file line numberDiff line numberDiff line change
@@ -18,11 +18,15 @@ var App = React.createClass({
1818
});
1919

2020
var User = React.createClass({
21-
mixins: [ Router.State ],
21+
22+
contextTypes: {
23+
router: React.PropTypes.func.isRequired
24+
},
2225

2326
render: function () {
24-
var age = this.getQuery().showAge ? '33' : '';
25-
var userID = this.getParams().userID;
27+
var { router } = this.context;
28+
var age = router.getCurrentQuery().showAge ? '33' : '';
29+
var userID = router.getCurrentParams().userID;
2630
return (
2731
<div className="User">
2832
<h1>User id: {userID}</h1>

examples/rx/app.js

+5-2
Original file line numberDiff line numberDiff line change
@@ -18,10 +18,13 @@ var App = React.createClass({
1818
});
1919

2020
var User = React.createClass({
21-
mixins: [ Router.State ],
21+
22+
contextTypes: {
23+
router: React.PropTypes.func.isRequired
24+
},
2225

2326
render () {
24-
var { userId } = this.getParams();
27+
var { userId } = this.context.router.getCurrentParams();
2528
return (
2629
<div className="User">
2730
<h1>User id: {userId}</h1>

examples/sidebar/app.js

+10-4
Original file line numberDiff line numberDiff line change
@@ -73,10 +73,13 @@ var Sidebar = React.createClass({
7373
});
7474

7575
var App = React.createClass({
76-
mixins: [ Router.State ],
76+
77+
contextTypes: {
78+
router: React.PropTypes.func.isRequired
79+
},
7780

7881
render: function () {
79-
var activeCategory = this.getParams().category;
82+
var activeCategory = this.context.router.getCurrentParams().category;
8083
return (
8184
<div>
8285
<Sidebar activeCategory={activeCategory} categories={data.getAll()}/>
@@ -89,10 +92,13 @@ var App = React.createClass({
8992
});
9093

9194
var Item = React.createClass({
92-
mixins: [ Router.State ],
95+
96+
contextTypes: {
97+
router: React.PropTypes.func.isRequired
98+
},
9399

94100
render: function () {
95-
var params = this.getParams();
101+
var params = this.context.router.getCurrentParams();
96102
var category = data.lookupCategory(params.category);
97103
var item = data.lookupItem(params.category, params.name);
98104
return (

examples/simple-master-detail/app.js

+5-2
Original file line numberDiff line numberDiff line change
@@ -38,14 +38,17 @@ var Index = React.createClass({
3838
});
3939

4040
var State = React.createClass({
41-
mixins: [ Router.State ],
41+
42+
contextTypes: {
43+
router: React.PropTypes.func.isRequired
44+
},
4245

4346
imageUrl: function (name) {
4447
return "http://www.50states.com/maps/" + underscore(name) + ".gif";
4548
},
4649

4750
render: function () {
48-
var unitedState = findState(this.getParams().abbr);
51+
var unitedState = findState(this.context.router.getCurrentParams().abbr);
4952
return (
5053
<div className="State">
5154
<h1>{unitedState.name}</h1>

examples/transitions/app.js

+4-2
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,9 @@ var Dashboard = React.createClass({
3030

3131
var Form = React.createClass({
3232

33-
mixins: [ Router.Navigation ],
33+
contextTypes: {
34+
router: React.PropTypes.func.isRequired
35+
},
3436

3537
statics: {
3638
willTransitionFrom: function (transition, element) {
@@ -45,7 +47,7 @@ var Form = React.createClass({
4547
handleSubmit: function (event) {
4648
event.preventDefault();
4749
this.refs.userInput.getDOMNode().value = '';
48-
this.transitionTo('/');
50+
this.context.router.transitionTo('/');
4951
},
5052

5153
render: function () {

examples/webpack.config.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ module.exports = {
2828

2929
module: {
3030
loaders: [
31-
{ test: /\.js$/, loader: 'babel-loader' }
31+
{ test: /\.js$/, exclude: /node_modules/, loader: 'babel-loader' }
3232
]
3333
},
3434

0 commit comments

Comments
 (0)