Guest User

Untitled

a guest
Apr 19th, 2018
90
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.70 KB | None | 0 0
  1. import React from "react";
  2. import pathToRegexp from "path-to-regexp";
  3.  
  4. function Home() {
  5. return <h1>Home</h1>;
  6. }
  7.  
  8. function About() {
  9. return <h1>About</h1>;
  10. }
  11.  
  12. function Contact() {
  13. return <h1>Contact</h1>;
  14. }
  15.  
  16. function Inbox({ match, params }) {
  17. return (
  18. <div>
  19. <h1>Inbox</h1>
  20. <Message key={params.id}/>
  21. <div>
  22. {params.id ? <p>Now showing message {params.id}</p> : null}
  23. </div>
  24. </div>
  25. );
  26. }
  27.  
  28. const RoutingContext = React.createContext();
  29.  
  30. class Router extends React.Component {
  31. state = {
  32. location: window.location
  33. };
  34.  
  35. push = url => {
  36. window.history.pushState(null, null, url);
  37. this.setState({ location: window.location });
  38. };
  39.  
  40. handlePopState = () => {
  41. this.setState({ location: window.location });
  42. };
  43.  
  44. componentDidMount() {
  45. window.addEventListener("popstate", this.handlePopState);
  46. }
  47.  
  48. componentWillUnmount() {
  49. window.removeEventListener("popstate", this.handlePopState);
  50. }
  51.  
  52. render() {
  53. const { location } = this.state;
  54.  
  55. return (
  56. <RoutingContext.Provider
  57. {...this.props}
  58. value={{ location, push: this.push }}
  59. />
  60. );
  61. }
  62. }
  63.  
  64. function Route({ path, component: Component }) {
  65. const keys = [];
  66. const regexp = pathToRegexp(path, keys);
  67.  
  68. return (
  69. <RoutingContext.Consumer>
  70. {({ location }) => {
  71. const match = regexp.exec(location.pathname);
  72.  
  73. if (match) {
  74. const params = keys.reduce((memo, key, index) => {
  75. memo[key.name] = match[index + 1];
  76. return memo;
  77. }, {});
  78.  
  79. return <Component match={match} params={params} />;
  80. }
  81.  
  82. return null;
  83. }}
  84. </RoutingContext.Consumer>
  85. );
  86. }
  87.  
  88. function Link(props) {
  89. const { to, ...anchorProps } = props;
  90.  
  91. return (
  92. <RoutingContext.Consumer>
  93. {({ push }) => (
  94. <a
  95. {...anchorProps}
  96. href={to}
  97. onClick={event => {
  98. event.preventDefault();
  99. push(to);
  100. }}
  101. />
  102. )}
  103. </RoutingContext.Consumer>
  104. );
  105. }
  106.  
  107. class App extends React.Component {
  108. render() {
  109. return (
  110. <Router>
  111. <div>
  112. <h1>Welcome to the app!</h1>
  113.  
  114. <nav>
  115. <ul>
  116. <li>
  117. <Link to="/about">About Us</Link>
  118. </li>
  119. <li>
  120. <Link to="/contact">Contact Us</Link>
  121. </li>
  122. <li>
  123. <Link to="/">Home</Link>
  124. </li>
  125. </ul>
  126. </nav>
  127.  
  128. <Redirect to="..."/>
  129.  
  130. <Route path="/about" component={About} />
  131. <Route path="/contact" component={Contact} />
  132. <ParentRoute path="/" component={Home} />
  133. </div>
  134. </Router>
  135. );
  136. }
  137. }
  138.  
  139. export default App;
Add Comment
Please, Sign In to add comment