-
-
Notifications
You must be signed in to change notification settings - Fork 15.2k
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
Closed
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
c8ab126
cleaning
bf0fafb
update to react-router-redux 4.x
46dd120
cleaning
6426b3f
cleaning
f7ec1a9
init
e50bcad
async-router example
0fd5f12
async-with-routing example
7393f90
cleaning
81b8006
cleaning
40f6ec6
delete universal-with-router based on counter example
25cfc5a
cleaning
41932f1
simplification of the routing
4dc3944
title update
53a594b
linting
2c42ba8
linting
d05f8d2
name and description update
0dc353f
async-universal example
30e4d80
generic index route
4033f7d
cleaning
35fcf27
generic index route
c91dccb
Layout.js removed
71d55cf
intermediate refactoring
9633ecf
cleaning
c8bfd67
move fetchdata into container
ad054bb
cleaning
a1f2d63
remove transform-class-properties
545b288
remove static keyword
a489327
missing space
d4604bb
typo
110489e
remove unused import
5b96f84
Remove hacks to alias Redux to src folder in examples
50ef70c
Per-file ESLint exceptions removed
bf3b17a
Layout.js renamed to App.js
6890c4a
Remove NoErrorsPlugin
38105ca
Remove NoErrorsPlugin
a424585
compile redux from source is needed here
6a5d6e6
App component with kind of header added
a56fcbb
indentation
2f3cad8
babel-preset-react-hmre removed
35469d4
es5 version for the sake of consistency
052c8d7
Merge https://github.com/reactjs/redux
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
{ | ||
presets: ["es2015", "react"] | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) { | ||
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) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
30
examples/universal-with-router/common/components/Counter.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
19
examples/universal-with-router/common/components/Layout.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
11
examples/universal-with-router/common/components/Sample.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
16
examples/universal-with-router/common/containers/Counter.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
24
examples/universal-with-router/common/store/configureStore.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
require('./client') |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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", | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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. There was a problem hiding this comment. Choose a reason for hiding this commentThe 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" | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
require('babel-register') | ||
require('./server') |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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
.