-
-
Save codecademydev/b5c865cd6f70da10cc108035b139ef41 to your computer and use it in GitHub Desktop.
Codecademy export
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
import React from 'react'; | |
import ReactDOM from 'react-dom'; | |
import { createStore } from 'redux'; | |
// REDUX CODE | |
/////////////////////////////////// | |
const increment = () => { | |
return {type: 'increment'} | |
} | |
const decrement = () => { | |
return {type: 'decrement'} | |
} | |
const initialState = 0; | |
const counterReducer = (state = initialState, action) => { | |
switch (action.type) { | |
case 'increment': | |
return state + 1; | |
case 'decrement': | |
return state - 1; | |
default: | |
return state; | |
} | |
} | |
const store = createStore(counterReducer); | |
// REACT CODE | |
/////////////////////////////////// | |
const render = () => { | |
ReactDOM.render( | |
<CounterApp | |
state={store.getState()} | |
/>, | |
document.getElementById('root') | |
) | |
} | |
render(); | |
// Render once with the initial state. | |
// Subscribe render to changes to the store's state. | |
function CounterApp(props) { | |
const state = props.state | |
const onIncrementButtonClicked = () => { | |
store.dispatch(increment()) | |
} | |
const onDecrementButtonClicked = () => { | |
store.dispatch(decrement()) | |
} | |
return ( | |
<div id='counter-app'> | |
<h1> {state} </h1> | |
<button onClick={onIncrementButtonClicked}>+</button> | |
<button onClick={onDecrementButtonClicked}>-</button> | |
</div> | |
) | |
} | |
store.subscribe(render); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment