forked from reduxjs/redux
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request reduxjs#1288 from rackt/simplify-counter
Simplify counter example
- Loading branch information
Showing
12 changed files
with
95 additions
and
269 deletions.
There are no files selected for viewing
This file was deleted.
Oops, something went wrong.
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 |
---|---|---|
@@ -1,30 +1,52 @@ | ||
import React, { Component, PropTypes } from 'react' | ||
|
||
class Counter extends Component { | ||
constructor(props) { | ||
super(props) | ||
this.incrementAsync = this.incrementAsync.bind(this) | ||
this.incrementIfOdd = this.incrementIfOdd.bind(this) | ||
} | ||
|
||
incrementIfOdd() { | ||
if (this.props.value % 2 === 1) { | ||
this.props.onIncrement() | ||
} | ||
} | ||
|
||
incrementAsync() { | ||
setTimeout(this.props.onIncrement, 1000) | ||
} | ||
|
||
render() { | ||
const { increment, incrementIfOdd, incrementAsync, decrement, counter } = this.props | ||
const { value, onIncrement, onDecrement } = this.props | ||
return ( | ||
<p> | ||
Clicked: {counter} times | ||
Clicked: {value} times | ||
{' '} | ||
<button onClick={increment}>+</button> | ||
<button onClick={onIncrement}> | ||
+ | ||
</button> | ||
{' '} | ||
<button onClick={decrement}>-</button> | ||
<button onClick={onDecrement}> | ||
- | ||
</button> | ||
{' '} | ||
<button onClick={incrementIfOdd}>Increment if odd</button> | ||
<button onClick={this.incrementIfOdd}> | ||
Increment if odd | ||
</button> | ||
{' '} | ||
<button onClick={() => incrementAsync()}>Increment async</button> | ||
<button onClick={this.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 | ||
value: PropTypes.number.isRequired, | ||
onIncrement: PropTypes.func.isRequired, | ||
onDecrement: PropTypes.func.isRequired | ||
} | ||
|
||
export default Counter |
This file was deleted.
Oops, something went wrong.
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 |
---|---|---|
@@ -1,14 +1,22 @@ | ||
import React from 'react' | ||
import { render } from 'react-dom' | ||
import { Provider } from 'react-redux' | ||
import App from './containers/App' | ||
import configureStore from './store/configureStore' | ||
import ReactDOM from 'react-dom' | ||
import { createStore } from 'redux' | ||
import Counter from './components/Counter' | ||
import counter from './reducers' | ||
|
||
const store = configureStore() | ||
const store = createStore(counter) | ||
const rootEl = document.getElementById('root') | ||
|
||
render( | ||
<Provider store={store}> | ||
<App /> | ||
</Provider>, | ||
document.getElementById('root') | ||
) | ||
function render() { | ||
ReactDOM.render( | ||
<Counter | ||
value={store.getState()} | ||
onIncrement={() => store.dispatch({ type: 'INCREMENT' })} | ||
onDecrement={() => store.dispatch({ type: 'DECREMENT' })} | ||
/>, | ||
rootEl | ||
) | ||
} | ||
|
||
render() | ||
store.subscribe(render) |
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
This file was deleted.
Oops, something went wrong.
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 |
---|---|---|
@@ -1,8 +1,10 @@ | ||
import { combineReducers } from 'redux' | ||
import counter from './counter' | ||
|
||
const rootReducer = combineReducers({ | ||
counter | ||
}) | ||
|
||
export default rootReducer | ||
export default function counter(state = 0, action) { | ||
switch (action.type) { | ||
case 'INCREMENT': | ||
return state + 1 | ||
case 'DECREMENT': | ||
return state - 1 | ||
default: | ||
return state | ||
} | ||
} |
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
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
Oops, something went wrong.