├── .babelrc ├── .gitignore ├── .idea ├── jsLibraryMappings.xml ├── misc.xml ├── modules.xml ├── scotch-react-router.iml └── workspace.xml ├── README.md ├── index.js ├── package.json ├── public ├── bundle.js ├── bundle.js.map ├── index.html └── style.css ├── src ├── Car │ ├── car-detail.component.jsx │ └── car.component.jsx ├── Common │ ├── about.component.jsx │ ├── home.component.jsx │ └── main.component.jsx └── index.jsx └── webpack.config.js /.babelrc: -------------------------------------------------------------------------------- 1 | { 2 | "presets" : ["es2015", "react"] 3 | } -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | node_modules -------------------------------------------------------------------------------- /.idea/jsLibraryMappings.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | -------------------------------------------------------------------------------- /.idea/misc.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | -------------------------------------------------------------------------------- /.idea/modules.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | -------------------------------------------------------------------------------- /.idea/scotch-react-router.iml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | -------------------------------------------------------------------------------- /.idea/workspace.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 14 | 15 | 16 | 18 | 19 | 20 | 21 | 22 | 23 | 24 | 25 | 26 | 27 | 28 | 29 | 30 | 31 | 32 | 33 | 34 | 35 | 36 | 37 | 38 | 39 | 40 | 41 | 42 | 43 | 44 | 45 | 46 | 47 | 48 | 49 | 50 | 51 | 52 | 53 | 54 | 55 | 56 | 57 | 58 | 59 | 60 | 61 | 62 | 63 | 64 | 65 | 66 | 67 | 68 | 69 | 70 | 71 | 72 | 73 | 74 | 75 | 76 | 77 | 78 | 79 | 80 | 81 | 82 | 83 | 84 | 85 | 86 | 87 | 88 | 89 | 90 | 91 | 92 | 93 | 94 | 95 | 96 | 97 | 98 | 99 | 100 | 101 | 102 | 103 | 104 | 105 | 106 | 107 | 108 | 109 | 110 | 111 | 112 | 113 | 114 | 115 | 116 | 117 | 118 | 119 | 120 | 121 | 122 | 123 | 124 | 125 | 126 | 127 | 128 | 129 | 130 | 131 | 132 | 133 | 134 | 135 | 136 | 137 | 138 | 139 | 163 | 164 | 165 | 166 | 167 | true 168 | DEFINITION_ORDER 169 | 170 | 171 | 176 | 177 | 178 | 179 | 180 | 181 | 182 | 183 | 184 | 185 | 186 | 187 | 188 | 189 | 190 | 191 | 192 | 193 | 194 | 195 | 196 | 197 | 198 | 199 | 200 | 201 | 202 | 203 | 204 | 205 | 206 | 209 | 210 | 211 | 212 | 215 | 216 | 219 | 220 | 221 | 222 | 225 | 226 | 229 | 230 | 233 | 234 | 235 | 236 | 239 | 240 | 243 | 244 | 247 | 248 | 251 | 252 | 253 | 254 | 257 | 258 | 261 | 262 | 265 | 266 | 269 | 270 | 271 | 272 | 275 | 276 | 279 | 280 | 283 | 284 | 285 | 286 | 287 | 288 | 289 | 290 | 291 | 292 | 293 | 294 | 295 | 296 | 297 | 298 | 299 | 300 | 301 | 302 | 303 | 304 | 305 | 306 | 307 | 308 | 309 | 310 | 311 | 312 | 313 | 314 | 315 | 316 | 317 | 318 | 319 | 320 | 321 | 322 | 323 | 324 | 329 | 330 | 331 | 332 | 333 | 334 | 335 | 336 | 337 | 338 | 339 | 340 | project 341 | 342 | 343 | true 344 | 345 | bdd 346 | 347 | DIRECTORY 348 | 349 | false 350 | 351 | 352 | 353 | 354 | 356 | 357 | 358 | 359 | 1471378158860 360 | 365 | 366 | 367 | 368 | 370 | 371 | 372 | 373 | 374 | 375 | 376 | 377 | 378 | 379 | 380 | 381 | 382 | 383 | 384 | 385 | 386 | 387 | 388 | 389 | 390 | 391 | 392 | 393 | 395 | 396 | 397 | 398 | 399 | 400 | 401 | 402 | 403 | 404 | 405 | 406 | 407 | 408 | 409 | 410 | 411 | 412 | 413 | 414 | 415 | 416 | 417 | 418 | 419 | 420 | 421 | 422 | 423 | 424 | 425 | 426 | 427 | 428 | 429 | 430 | 431 | 432 | 433 | 434 | 435 | 436 | 437 | 438 | 439 | 440 | 441 | 442 | 443 | 444 | 445 | 446 | 447 | 448 | 449 | 450 | 451 | 452 | 453 | 454 | 455 | 456 | 457 | 458 | 459 | 460 | 461 | 462 | 463 | 464 | 465 | 466 | 467 | 468 | 469 | 470 | 471 | 472 | 473 | 474 | 475 | 476 | 477 | 478 | 479 | 480 | 481 | 482 | 483 | 484 | 485 | 486 | 487 | 488 | 489 | 490 | 491 | 492 | 493 | 494 | 495 | 496 | 497 | 498 | 499 | 500 | 501 | 502 | 503 | 504 | 505 | 506 | 507 | 508 | 509 | 510 | 511 | 512 | 513 | 514 | 515 | 516 | 517 | 518 | 519 | 520 | 521 | 522 | 523 | 524 | 525 | 526 | 527 | 528 | 529 | 530 | 531 | 532 | 533 | 534 | 535 | 536 | 537 | 538 | 539 | 540 | 541 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | ## Scotch React Router 2 | A demo that backs a Scotch article on routing in React. 3 | -------------------------------------------------------------------------------- /index.js: -------------------------------------------------------------------------------- 1 | const express = require('express') 2 | const path = require('path') 3 | const port = process.env.PORT || 3000 4 | const app = express() 5 | 6 | // serve static assets normally 7 | app.use(express.static(__dirname + '/public')) 8 | 9 | // Handles all routes so you do not get a not found error 10 | app.get('*', function (request, response){ 11 | response.sendFile(path.resolve(__dirname, 'public', 'index.html')) 12 | }) 13 | 14 | app.listen(port) 15 | console.log("server started on port " + port) 16 | -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "scotch-react-router", 3 | "version": "1.0.0", 4 | "description": "React Router Aricle for Scotch", 5 | "main": "index.js", 6 | "scripts": { 7 | "watch": "webpack -d --watch", 8 | "build": "webpack", 9 | "serve": "serve ./public", 10 | "start": "nodemon" 11 | }, 12 | "author": "Christian Nwamba", 13 | "license": "MIT", 14 | "devDependencies": { 15 | "babel-loader": "^6.2.4", 16 | "babel-preset-es2015": "^6.13.2", 17 | "babel-preset-react": "^6.11.1", 18 | "webpack": "^1.13.1", 19 | "serve": "^1.4.0" 20 | }, 21 | "dependencies": { 22 | "express": "^4.14.0", 23 | "history": "^3.0.0", 24 | "react": "^15.3.0", 25 | "react-dom": "^15.3.0", 26 | "react-router": "^2.6.1" 27 | } 28 | } 29 | -------------------------------------------------------------------------------- /public/index.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | Scotch Cars 5 | 6 | 7 | 8 | 9 | 10 | 11 |
12 | 13 | 14 | 15 | -------------------------------------------------------------------------------- /public/style.css: -------------------------------------------------------------------------------- 1 | a.active{ 2 | color: #000000 !important; 3 | text-decoration: underline !important; 4 | } 5 | -------------------------------------------------------------------------------- /src/Car/car-detail.component.jsx: -------------------------------------------------------------------------------- 1 | import React, { Component } from 'react'; 2 | import { browserHistory } from 'react-router'; 3 | 4 | class CarDetail extends Component { 5 | handleRedirect(){ 6 | browserHistory.push('/cars'); 7 | } 8 | render(){ 9 | const cars = this.props.route.data; 10 | const id = this.props.params.id; 11 | const car = cars.filter(car => { 12 | if(car.id == id) { 13 | return car; 14 | } 15 | }); 16 | 17 | return ( 18 |
19 |

{car[0].name}

20 |
21 |
22 |
23 | {car[0].name} 24 |
25 |
26 |
27 |
    28 |
  • Model: {car[0].model}
  • 29 |
  • Make: {car[0].make}
  • 30 |
  • Year: {car[0].year}
  • 31 |
  • Price: {car[0].price}
  • 32 |
33 |
34 |
35 | 36 |
37 |
38 |
39 | ); 40 | } 41 | } 42 | 43 | export default CarDetail -------------------------------------------------------------------------------- /src/Car/car.component.jsx: -------------------------------------------------------------------------------- 1 | import React, { Component } from 'react'; 2 | import { Link } from 'react-router'; 3 | 4 | class Car extends Component { 5 | render(){ 6 | // Get data from route props 7 | const cars = this.props.route.data; 8 | // Map through cars and return linked cars 9 | const carNode = cars.map((car) => { 10 | return ( 11 | 15 | {car.name} 16 | 17 | ) 18 | }); 19 | return ( 20 |
21 |

Cars page

22 |
23 | {carNode} 24 |
25 |
26 | ); 27 | } 28 | } 29 | 30 | export default Car 31 | -------------------------------------------------------------------------------- /src/Common/about.component.jsx: -------------------------------------------------------------------------------- 1 | import React, { Component } from 'react'; 2 | 3 | class About extends Component { 4 | render(){ 5 | return (

About Page

); 6 | } 7 | } 8 | 9 | export default About -------------------------------------------------------------------------------- /src/Common/home.component.jsx: -------------------------------------------------------------------------------- 1 | import React, { Component } from 'react'; 2 | 3 | class Home extends Component { 4 | render(){ 5 | return (

Home Page

); 6 | } 7 | } 8 | 9 | export default Home -------------------------------------------------------------------------------- /src/Common/main.component.jsx: -------------------------------------------------------------------------------- 1 | import React, {Component} from 'react'; 2 | import { Link } from 'react-router'; 3 | 4 | class Main extends Component { 5 | render(){ 6 | return( 7 |
8 | 23 |
24 | {this.props.children} 25 |
26 |
27 | ); 28 | } 29 | } 30 | 31 | export default Main -------------------------------------------------------------------------------- /src/index.jsx: -------------------------------------------------------------------------------- 1 | import React, { Component } from 'react'; 2 | import { render } from 'react-dom'; 3 | // Import routing components 4 | import {Router, Route, IndexRoute, browserHistory} from 'react-router'; 5 | 6 | import Main from './common/main.component.jsx' 7 | import Home from './common/home.component.jsx' 8 | import About from './common/about.component.jsx' 9 | import Car from './car/car.component.jsx' 10 | import CarDetail from './car/car-detail.component.jsx' 11 | 12 | const data = [ 13 | { 14 | id: 1, 15 | name: 'Honda Accord Crosstour', 16 | year: '2010', 17 | model: 'Accord Crosstour', 18 | make: 'Honda', 19 | media: 'http://media.ed.edmunds-media.com/honda/accord-crosstour/2010/oem/2010_honda_accord-crosstour_4dr-hatchback_ex-l_fq_oem_4_500.jpg', 20 | price: '$16,811' 21 | 22 | }, 23 | { 24 | id: 2, 25 | name: 'Mercedes-Benz AMG GT Coupe', 26 | year: '2016', 27 | model: 'AMG', 28 | make: 'Mercedes Benz', 29 | media: 'http://media.ed.edmunds-media.com/mercedes-benz/amg-gt/2016/oem/2016_mercedes-benz_amg-gt_coupe_s_fq_oem_1_717.jpg', 30 | price: '$138,157' 31 | 32 | }, 33 | { 34 | id: 3, 35 | name: 'BMW X6 SUV', 36 | year: '2016', 37 | model: 'X6', 38 | make: 'BMW', 39 | media: 'http://media.ed.edmunds-media.com/bmw/x6/2016/oem/2016_bmw_x6_4dr-suv_xdrive50i_fq_oem_1_717.jpg', 40 | price: '$68,999' 41 | }, 42 | { 43 | id: 4, 44 | name: 'Ford Edge SUV', 45 | year: '2016', 46 | model: 'Edge', 47 | make: 'Ford', 48 | media: 'http://media.ed.edmunds-media.com/ford/edge/2016/oem/2016_ford_edge_4dr-suv_sport_fq_oem_6_717.jpg', 49 | price: '$36,275' 50 | }, 51 | { 52 | id: 5, 53 | name: 'Dodge Viper Coupe', 54 | year: '2017', 55 | model: 'Viper', 56 | make: 'Dodge', 57 | media: 'http://media.ed.edmunds-media.com/dodge/viper/2017/oem/2017_dodge_viper_coupe_acr_fq_oem_3_717.jpg', 58 | price: '$123,890' 59 | } 60 | ]; 61 | 62 | 63 | render( 64 | 65 | 66 | 67 | 68 | {/* Parameter route*/} 69 | 70 | 71 | 72 | , 73 | document.getElementById('container') 74 | ); 75 | -------------------------------------------------------------------------------- /webpack.config.js: -------------------------------------------------------------------------------- 1 | var webpack = require('webpack'); 2 | var path = require('path'); 3 | 4 | var BUILD_DIR = path.resolve(__dirname, 'public'); 5 | var APP_DIR = path.resolve(__dirname, 'src'); 6 | 7 | var config = { 8 | entry: APP_DIR + '/index.jsx', 9 | output: { 10 | path: BUILD_DIR, 11 | filename: 'bundle.js' 12 | }, 13 | module : { 14 | loaders : [ 15 | { 16 | test : /\.jsx?/, 17 | include : APP_DIR, 18 | loader : 'babel' 19 | } 20 | ] 21 | } 22 | }; 23 | 24 | module.exports = config; --------------------------------------------------------------------------------