diff --git a/lessons/01-setting-up/index.js b/lessons/01-setting-up/index.js index 898eaa43..2e628b6a 100644 --- a/lessons/01-setting-up/index.js +++ b/lessons/01-setting-up/index.js @@ -1,4 +1,24 @@ import React from 'react' import { render } from 'react-dom' import App from './modules/App' -render(, document.getElementById('app')) +import { Router, Route, browserHistory, IndexRoute } from 'react-router' +import About from './modules/About' +import Repos from './modules/Repos' +import Repo from './modules/Repo' +import Home from './modules/Home' + +render(( + + + + + + + + + + + + +), document.getElementById('app')) +// render(, document.getElementById('app')) diff --git a/lessons/01-setting-up/modules/About.js b/lessons/01-setting-up/modules/About.js new file mode 100644 index 00000000..caa8a178 --- /dev/null +++ b/lessons/01-setting-up/modules/About.js @@ -0,0 +1,10 @@ +/** + * Created by oBlank on 7/2/16. + */ +import React from 'react' + +export default React.createClass({ + render() { + return
About
+ } +}) \ No newline at end of file diff --git a/lessons/01-setting-up/modules/App.js b/lessons/01-setting-up/modules/App.js index cbbc7217..54841e49 100644 --- a/lessons/01-setting-up/modules/App.js +++ b/lessons/01-setting-up/modules/App.js @@ -1,7 +1,30 @@ import React from 'react' +import {Link} from 'react-router' +import NavLink from './NavLink' +import Home from './Home' export default React.createClass({ render() { - return
Hello, React Router!
+ return ( +
+
Hello, React Router! xxx
+ +
  • Home
  • +
  • About
  • +
  • Repos
  • +
  • activeStyle
  • +
  • Repos activeStyle
  • +
  • activeClassName
  • +
  • Repos activeClassName
  • + +
  • 自定义链接
  • +
  • About NavLink
  • +
  • Repos NavLink
  • +
    +

    以下为内容:

    + + {this.props.children} +
    + ) } }) diff --git a/lessons/01-setting-up/modules/Home.js b/lessons/01-setting-up/modules/Home.js new file mode 100644 index 00000000..15f6de1c --- /dev/null +++ b/lessons/01-setting-up/modules/Home.js @@ -0,0 +1,10 @@ +/** + * Created by oBlank on 7/2/16. + */ +import React from 'react' + +export default React.createClass({ + render() { + return
    Home,请选择菜单
    + } +}) \ No newline at end of file diff --git a/lessons/01-setting-up/modules/NavLink.js b/lessons/01-setting-up/modules/NavLink.js new file mode 100644 index 00000000..007049f4 --- /dev/null +++ b/lessons/01-setting-up/modules/NavLink.js @@ -0,0 +1,11 @@ +/** + * Created by oBlank on 7/2/16. + */ +import React from 'react' +import { Link } from 'react-router' + +export default React.createClass({ + render() { + return + } +}) \ No newline at end of file diff --git a/lessons/01-setting-up/modules/Repo.js b/lessons/01-setting-up/modules/Repo.js new file mode 100644 index 00000000..77c2683e --- /dev/null +++ b/lessons/01-setting-up/modules/Repo.js @@ -0,0 +1,14 @@ +/** + * Created by oBlank on 7/2/16. + */ +import React from 'react' + +export default React.createClass({ + render() { + return ( +
    +
    name: {this.props.params.userName}
    repo:{this.props.params.repoName}
    +
    + ) + } +}) \ No newline at end of file diff --git a/lessons/01-setting-up/modules/Repos.js b/lessons/01-setting-up/modules/Repos.js new file mode 100644 index 00000000..bb90b06d --- /dev/null +++ b/lessons/01-setting-up/modules/Repos.js @@ -0,0 +1,49 @@ +/** + * Created by oBlank on 7/2/16. + */ +import React from 'react' +import { Link } from 'react-router' +import NavLink from './NavLink' +import { browserHistory } from 'react-router' + +export default React.createClass({ + + // ask for `router` from context + contextTypes: { + router: React.PropTypes.object + }, + + // add this method + handleSubmit(event) { + event.preventDefault() + const userName = event.target.elements[0].value + const repo = event.target.elements[1].value + const path = `/repos/${userName}/${repo}` + console.log(path) + // browserHistory.push(path) + this.context.router.push(path) + }, + + render() { + return ( +
    +
    Repos
    +
      +
    • React Router
    • +
    • React
    • +
    • 自定义NavLink:
    • +
    • React Router
    • +
    • React
    • +
    • +
      + / {' '} + {' '} + +
      +
    • +
    + {this.props.children} +
    + ) + } +}) \ No newline at end of file diff --git a/lessons/01-setting-up/package.json b/lessons/01-setting-up/package.json index 51792f8f..9b890a3d 100644 --- a/lessons/01-setting-up/package.json +++ b/lessons/01-setting-up/package.json @@ -4,11 +4,19 @@ "description": "", "main": "index.js", "scripts": { - "start": "webpack-dev-server --inline --content-base ." + "start": "if-env NODE_ENV=production && npm run start:prod || npm run start:dev", + "start:dev": "webpack-dev-server --inline --content-base public/ --history-api-fallback", + "start:prod": "npm run build && node server.bundle.js", + "build:client": "webpack", + "build:server": "webpack --config webpack.server.config.js", + "build": "npm run build:client && npm run build:server" }, "author": "", "license": "ISC", "dependencies": { + "compression": "^1.6.2", + "express": "^4.14.0", + "if-env": "^1.0.0", "react": "^0.14.7", "react-dom": "^0.14.7", "react-router": "^2.0.0" diff --git a/lessons/01-setting-up/public/index.css b/lessons/01-setting-up/public/index.css new file mode 100644 index 00000000..93cfadfc --- /dev/null +++ b/lessons/01-setting-up/public/index.css @@ -0,0 +1,3 @@ +.active { + color: green; +} \ No newline at end of file diff --git a/lessons/01-setting-up/index.html b/lessons/01-setting-up/public/index.html similarity index 60% rename from lessons/01-setting-up/index.html rename to lessons/01-setting-up/public/index.html index 0a661421..08a53ccb 100644 --- a/lessons/01-setting-up/index.html +++ b/lessons/01-setting-up/public/index.html @@ -2,5 +2,6 @@ My First React Router App +
    - + diff --git a/lessons/01-setting-up/server.js b/lessons/01-setting-up/server.js new file mode 100644 index 00000000..d63b15c0 --- /dev/null +++ b/lessons/01-setting-up/server.js @@ -0,0 +1,21 @@ +/** + * Created by oBlank on 7/2/16. + */ +var express = require('express') +var path = require('path') +var compression = require('compression') + +var app = express() + +// serve our static stuff like index.css +app.use(express.static(path.join(__dirname, 'public'))) + +// send all requests to index.html so browserHistory in React Router works +app.get('*', function (req, res) { + res.sendFile(path.join(__dirname, 'public', 'index.html')) +}) + +var PORT = process.env.PORT || 8080 +app.listen(PORT, function() { + console.log('Production Express server running at localhost:' + PORT) +}) \ No newline at end of file diff --git a/lessons/01-setting-up/webpack.config.js b/lessons/01-setting-up/webpack.config.js index c5866620..faa3b58a 100644 --- a/lessons/01-setting-up/webpack.config.js +++ b/lessons/01-setting-up/webpack.config.js @@ -2,6 +2,7 @@ module.exports = { entry: './index.js', output: { + path: 'public', filename: 'bundle.js', publicPath: '' }, diff --git a/lessons/01-setting-up/webpack.server.config.js b/lessons/01-setting-up/webpack.server.config.js new file mode 100644 index 00000000..af4ab2eb --- /dev/null +++ b/lessons/01-setting-up/webpack.server.config.js @@ -0,0 +1,36 @@ +/** + * Created by oBlank on 7/2/16. + */ +var fs = require('fs') +var path = require('path') + +module.exports = { + + entry: path.resolve(__dirname, 'server.js'), + + output: { + filename: 'server.bundle.js' + }, + + target: 'node', + + // keep node_module paths out of the bundle + externals: fs.readdirSync(path.resolve(__dirname, 'node_modules')).concat([ + 'react-dom/server', 'react/addons', + ]).reduce(function (ext, mod) { + ext[mod] = 'commonjs ' + mod + return ext + }, {}), + + node: { + __filename: true, + __dirname: true + }, + + module: { + loaders: [ + { test: /\.js$/, exclude: /node_modules/, loader: 'babel-loader?presets[]=es2015&presets[]=react' } + ] + } + +} \ No newline at end of file diff --git a/lessons/02-rendering-a-route/index.js b/lessons/02-rendering-a-route/index.js index 898eaa43..f3fcb3d6 100644 --- a/lessons/02-rendering-a-route/index.js +++ b/lessons/02-rendering-a-route/index.js @@ -1,4 +1,11 @@ import React from 'react' import { render } from 'react-dom' import App from './modules/App' -render(, document.getElementById('app')) +import { Router, Route, hashHistory } from 'react-router' + +render(( + + + +), document.getElementById('app')) +// render(, document.getElementById('app'))