Skip to content

Instantly share code, notes, and snippets.

@azamsharp
Created July 26, 2018 13:53
Show Gist options
  • Save azamsharp/04c8fb2d91c52ac7e0f1701fc14d11a1 to your computer and use it in GitHub Desktop.
Save azamsharp/04c8fb2d91c52ac7e0f1701fc14d11a1 to your computer and use it in GitHub Desktop.
import { createStore, combineReducers } from 'redux'
const rootReducer = combineReducers({
ctr : counterReducer,
res : resultReducer
})
// map state to properties
const mapStateToProps = (state) => {
return {
// counter will be available as a property
// and can be accesed using this.props.counter
ctr : state.ctr.counter,
results : state.res.counterHistory
}
}
import * as actionTypes from '../actions/actionTypes'
const initialState = {
counter : 567
}
const reducer = (state = initialState,action) => {
switch(action.type) {
case actionTypes.INCREMENT_COUNTER:
return {
...state,
counter : state.counter + 1
}
case actionTypes.DECREMENT_COUNTER:
return {
...state,
counter : state.counter - 1
}
case actionTypes.ADD_COUNTER:
return {
...state,
counter : state.counter + action.value
}
}
return state
}
export default reducer
import * as actionTypes from './actionTypes'
export const addMovie = (movie) => {
return {
type : actionTypes.ADD_MOVIE,
movie : movie
}
}
export const fetchPosts = () => {
return dispatch => {
fetch('https://jsonplaceholder.typicode.com/posts')
.then(response => response.json())
.then((json) => {
dispatch({type : actionTypes.FETCH_POSTS, posts : json})
})
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment