|
1 | | -import React, { findDOMNode } from 'react'; |
2 | | -import { Router, Route, Link, Navigation, TransitionHook } from 'react-router'; |
3 | | -import { history } from 'react-router/lib/HashHistory'; |
| 1 | +import React from 'react'; |
| 2 | +import createHistory from 'history/lib/createHashHistory'; |
| 3 | +import { Router, Route, Link, Navigation } from 'react-router'; |
| 4 | + |
| 5 | +var history = createHistory(); |
4 | 6 |
|
5 | 7 | var App = React.createClass({ |
6 | 8 | render() { |
@@ -29,26 +31,51 @@ var Dashboard = React.createClass({ |
29 | 31 | }); |
30 | 32 |
|
31 | 33 | var Form = React.createClass({ |
32 | | - mixins: [ Navigation, TransitionHook ], |
| 34 | + mixins: [ Navigation ], |
| 35 | + |
| 36 | + getInitialState() { |
| 37 | + return { |
| 38 | + textValue: 'ohai' |
| 39 | + }; |
| 40 | + }, |
33 | 41 |
|
34 | | - routerWillLeave(nextState, transition) { |
35 | | - if (findDOMNode(this.refs.userInput).value !== '') |
36 | | - if (!confirm('You have unsaved information, are you sure you want to leave this page?')) |
37 | | - transition.abort(); |
| 42 | + transitionHook() { |
| 43 | + if (this.state.textValue) |
| 44 | + return 'You have unsaved information, are you sure you want to leave this page?'; |
| 45 | + }, |
| 46 | + |
| 47 | + componentDidMount() { |
| 48 | + history.registerTransitionHook(this.transitionHook); |
| 49 | + }, |
| 50 | + |
| 51 | + componentWillUnmount() { |
| 52 | + history.unregisterTransitionHook(this.transitionHook); |
| 53 | + }, |
| 54 | + |
| 55 | + handleChange(event) { |
| 56 | + var { value } = event.target; |
| 57 | + |
| 58 | + this.setState({ |
| 59 | + textValue: value |
| 60 | + }); |
38 | 61 | }, |
39 | 62 |
|
40 | 63 | handleSubmit(event) { |
41 | 64 | event.preventDefault(); |
42 | | - findDOMNode(this.refs.userInput).value = ''; |
43 | | - this.transitionTo('/'); |
| 65 | + |
| 66 | + this.setState({ |
| 67 | + textValue: '' |
| 68 | + }, () => { |
| 69 | + this.transitionTo('/'); |
| 70 | + }); |
44 | 71 | }, |
45 | 72 |
|
46 | 73 | render() { |
47 | 74 | return ( |
48 | 75 | <div> |
49 | 76 | <form onSubmit={this.handleSubmit}> |
50 | 77 | <p>Click the dashboard link with text in the input.</p> |
51 | | - <input type="text" ref="userInput" defaultValue="ohai" /> |
| 78 | + <input type="text" ref="userInput" value={this.state.textValue} onChange={this.handleChange} /> |
52 | 79 | <button type="submit">Go</button> |
53 | 80 | </form> |
54 | 81 | </div> |
|
0 commit comments