Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Replace universal example with async-with-routing and async-universal #1403

Closed
wants to merge 42 commits into from
Closed
Show file tree
Hide file tree
Changes from 2 commits
Commits
Show all changes
42 commits
Select commit Hold shift + click to select a range
3ddb19f
basic universal example with react-router and react-router-redux
Feb 15, 2016
c8ab126
cleaning
Feb 15, 2016
bf0fafb
update to react-router-redux 4.x
Feb 15, 2016
46dd120
cleaning
Feb 15, 2016
6426b3f
cleaning
Feb 15, 2016
f7ec1a9
init
Feb 17, 2016
e50bcad
async-router example
Feb 17, 2016
0fd5f12
async-with-routing example
Feb 17, 2016
7393f90
cleaning
Feb 17, 2016
81b8006
cleaning
Feb 17, 2016
40f6ec6
delete universal-with-router based on counter example
Feb 17, 2016
25cfc5a
cleaning
Feb 17, 2016
41932f1
simplification of the routing
Feb 17, 2016
4dc3944
title update
Feb 17, 2016
53a594b
linting
Feb 17, 2016
2c42ba8
linting
Feb 18, 2016
d05f8d2
name and description update
Feb 18, 2016
0dc353f
async-universal example
Feb 18, 2016
30e4d80
generic index route
Feb 23, 2016
4033f7d
cleaning
Feb 23, 2016
35fcf27
generic index route
Feb 23, 2016
c91dccb
Layout.js removed
Mar 11, 2016
71d55cf
intermediate refactoring
Mar 11, 2016
9633ecf
cleaning
Mar 11, 2016
c8bfd67
move fetchdata into container
Mar 11, 2016
ad054bb
cleaning
Mar 11, 2016
a1f2d63
remove transform-class-properties
Apr 12, 2016
545b288
remove static keyword
Apr 12, 2016
a489327
missing space
Apr 12, 2016
d4604bb
typo
Apr 12, 2016
110489e
remove unused import
Apr 12, 2016
5b96f84
Remove hacks to alias Redux to src folder in examples
Apr 12, 2016
50ef70c
Per-file ESLint exceptions removed
Apr 12, 2016
bf3b17a
Layout.js renamed to App.js
Apr 12, 2016
6890c4a
Remove NoErrorsPlugin
Apr 12, 2016
38105ca
Remove NoErrorsPlugin
Apr 12, 2016
a424585
compile redux from source is needed here
Apr 12, 2016
6a5d6e6
App component with kind of header added
Apr 12, 2016
a56fcbb
indentation
Apr 12, 2016
2f3cad8
babel-preset-react-hmre removed
Apr 12, 2016
35469d4
es5 version for the sake of consistency
Apr 14, 2016
052c8d7
Merge https://github.com/reactjs/redux
Apr 21, 2016
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions examples/universal-with-router/.babelrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
presets: ["es2015", "react"]
}
25 changes: 25 additions & 0 deletions examples/universal-with-router/client/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
import 'babel-polyfill'
import React from 'react'
import { render } from 'react-dom'
import { Provider } from 'react-redux'
import { Router, browserHistory } from 'react-router'
import { syncHistory } from 'react-router-redux'

import configureStore from '../common/store/configureStore'
import routes from '../common/routes'

const initialState = window.__INITIAL_STATE__
const reduxRouterMiddleware = syncHistory(browserHistory)
const store = configureStore(initialState, [ reduxRouterMiddleware ])
const rootElement = document.getElementById('app')

reduxRouterMiddleware.listenForReplays(store)

render(
<Provider store={store}>
<Router history={browserHistory}>
{routes}
</Router>
</Provider>,
rootElement
)
42 changes: 42 additions & 0 deletions examples/universal-with-router/common/actions/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
export const SET_COUNTER = 'SET_COUNTER'
export const INCREMENT_COUNTER = 'INCREMENT_COUNTER'
export const DECREMENT_COUNTER = 'DECREMENT_COUNTER'

export function set(value) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let’s kill all of this with fire. I don’t think the “counter” example makes any sense for the universal example. Instead we should copy the components and logic form async and fetch that in the new (supposedly renamed) async-universal.

return {
type: SET_COUNTER,
payload: value
}
}

export function increment() {
return {
type: INCREMENT_COUNTER
}
}

export function decrement() {
return {
type: DECREMENT_COUNTER
}
}

export function incrementIfOdd() {
return (dispatch, getState) => {
const { counter } = getState()

if (counter % 2 === 0) {
return
}

dispatch(increment())
}
}

export function incrementAsync(delay = 1000) {
return dispatch => {
setTimeout(() => {
dispatch(increment())
}, delay)
}
}
13 changes: 13 additions & 0 deletions examples/universal-with-router/common/api/counter.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
function getRandomInt(min, max) {
return Math.floor(Math.random() * (max - min)) + min
}

export function fetchCounter(callback) {
// Rather than immediately returning, we delay our code with a timeout to simulate asynchronous behavior
setTimeout(() => {
callback(getRandomInt(1, 100))
}, 500)

// In the case of a real world API call, you'll normally run into a Promise like this:
// API.getUser().then(user => callback(user))
}
30 changes: 30 additions & 0 deletions examples/universal-with-router/common/components/Counter.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
import React, { Component, PropTypes } from 'react'

class Counter extends Component {
render() {
const { increment, incrementIfOdd, incrementAsync, decrement, counter } = this.props
return (
<p>
Clicked: {counter} times
{' '}
<button onClick={increment}>+</button>
{' '}
<button onClick={decrement}>-</button>
{' '}
<button onClick={incrementIfOdd}>Increment if odd</button>
{' '}
<button onClick={() => incrementAsync()}>Increment async</button>
</p>
)
}
}

Counter.propTypes = {
increment: PropTypes.func.isRequired,
incrementIfOdd: PropTypes.func.isRequired,
incrementAsync: PropTypes.func.isRequired,
decrement: PropTypes.func.isRequired,
counter: PropTypes.number.isRequired
}

export default Counter
11 changes: 11 additions & 0 deletions examples/universal-with-router/common/components/Home.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import React, { Component } from 'react'

export default class Home extends Component {
render() {
return (
<div>
<p>Home page</p>
</div>
)
}
}
19 changes: 19 additions & 0 deletions examples/universal-with-router/common/components/Layout.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import React, { Component } from 'react'
import { Link } from 'react-router'

export default class Layout extends Component {
render() {
return (
<div>
<nav>
<ul>
<li><Link to="/">Home</Link></li>
<li><Link to="/sample">Sample</Link></li>
<li><Link to="/counter">Counter</Link></li>
</ul>
</nav>
{this.props.children}
</div>
)
}
}
11 changes: 11 additions & 0 deletions examples/universal-with-router/common/components/Sample.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import React, { Component } from 'react'

export default class Sample extends Component {
render() {
return (
<div>
<p>Sample</p>
</div>
)
}
}
16 changes: 16 additions & 0 deletions examples/universal-with-router/common/containers/Counter.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import { bindActionCreators } from 'redux'
import { connect } from 'react-redux'
import Counter from '../components/Counter'
import * as CounterActions from '../actions'

function mapStateToProps(state) {
return {
counter: state.counter
}
}

function mapDispatchToProps(dispatch) {
return bindActionCreators(CounterActions, dispatch)
}

export default connect(mapStateToProps, mapDispatchToProps)(Counter)
14 changes: 14 additions & 0 deletions examples/universal-with-router/common/reducers/counter.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import { SET_COUNTER, INCREMENT_COUNTER, DECREMENT_COUNTER } from '../actions'

export default function counter(state = 0, action) {
switch (action.type) {
case SET_COUNTER:
return action.payload
case INCREMENT_COUNTER:
return state + 1
case DECREMENT_COUNTER:
return state - 1
default:
return state
}
}
10 changes: 10 additions & 0 deletions examples/universal-with-router/common/reducers/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import { combineReducers } from 'redux'
import counter from './counter'
import { routeReducer } from 'react-router-redux'

const rootReducer = combineReducers({
routing: routeReducer,
counter
})

export default rootReducer
15 changes: 15 additions & 0 deletions examples/universal-with-router/common/routes.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import React from 'react'
import { Route, IndexRoute } from 'react-router'

import Layout from './components/Layout'
import Home from './components/Home'
import Sample from './components/Sample'
import Counter from './containers/Counter'

export default (
<Route path="/" component={Layout}>
<IndexRoute component={Home}/>
<Route path="sample" component={Sample}/>
<Route path="counter" component={Counter}/>
</Route>
)
24 changes: 24 additions & 0 deletions examples/universal-with-router/common/store/configureStore.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
import { createStore, applyMiddleware } from 'redux'
import thunk from 'redux-thunk'
import rootReducer from '../reducers'

export default function configureStore(initialState, middlewares = []) {

middlewares = [ thunk, ...middlewares ]

const store = createStore(
rootReducer,
initialState,
applyMiddleware(...middlewares)
)

if (module.hot) {
// Enable Webpack hot module replacement for reducers
module.hot.accept('../reducers', () => {
const nextRootReducer = require('../reducers').default
store.replaceReducer(nextRootReducer)
})
}

return store
}
1 change: 1 addition & 0 deletions examples/universal-with-router/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
require('./client')
42 changes: 42 additions & 0 deletions examples/universal-with-router/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
{
"name": "redux-universal-example",
"version": "0.0.0",
"description": "An example of a universally-rendered Redux application",
"scripts": {
"start": "node server/index.js"
},
"repository": {
"type": "git",
"url": "git+https://github.com/reactjs/redux.git"
},
"license": "MIT",
"bugs": {
"url": "https://github.com/reactjs/redux/issues"
},
"homepage": "http://redux.js.org",
"dependencies": {
"babel-polyfill": "^6.3.14",
"babel-register": "^6.4.3",
"express": "^4.13.3",
"qs": "^4.0.0",
"react": "^0.14.7",
"react-dom": "^0.14.7",
"react-redux": "^4.2.1",
"react-router": "^2.0.0",
"react-router-redux": "^3.0.0",
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we please use 4.x instead? I think there’s a beta out. It has a slightly different API but it’s better.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

sure, it's done

"redux": "^3.2.1",
"redux-thunk": "^1.0.3",
"serve-static": "^1.10.0"
},
"devDependencies": {
"babel-core": "^6.3.15",
"babel-loader": "^6.2.0",
"babel-preset-es2015": "^6.3.13",
"babel-preset-react": "^6.3.13",
"babel-preset-react-hmre": "^1.0.1",
"babel-runtime": "^6.3.13",
"webpack": "^1.11.0",
"webpack-dev-middleware": "^1.4.0",
"webpack-hot-middleware": "^2.6.0"
}
}
2 changes: 2 additions & 0 deletions examples/universal-with-router/server/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
require('babel-register')
require('./server')
Loading