Guest User

Untitled

a guest
Jul 18th, 2018
70
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.72 KB | None | 0 0
  1. // #01 creating action file (./src/js/actions/courseActions.js)
  2. export function createCourse(course) {
  3. return { type: 'CREATE_COURSE' , course }
  4. }
  5.  
  6.  
  7.  
  8.  
  9. // #02 creating reducer file (./src/js/reducers/courseReducer.js)
  10. export default function courseReducer(state = [] , action) {
  11. switch(action.type) {
  12. case 'CREATE_COURSE':
  13. return [
  14. ...state ,
  15. Object.assign({} , action.course)
  16. ]
  17.  
  18. default:
  19. return state;
  20. }
  21. }
  22.  
  23.  
  24.  
  25.  
  26. // #03 creating root reducer file named as index.js in reducers folder (./src/js/reducers/index.js)
  27. import { combineReducers } from 'redux'
  28. import courseReducer from './courseReducer'
  29.  
  30. const rootReducer = combineReducers({
  31. // this part of state == must handled by == this reducer
  32. course: courseReducer
  33. })
  34.  
  35. export default rootReducer;
  36.  
  37.  
  38.  
  39.  
  40.  
  41. // #04 creating configureStore file in store folder (./src/js/store/configureStore.js)
  42. import { createStore , applyMiddleware } from 'redux'
  43. import rootReducer from '../reducers/index'
  44. // import reduxImmutableStateInvarient from '@types/redux-immutable-state-invariant'
  45.  
  46.  
  47. export default function configureStore(initialState) {
  48. return createStore(
  49. rootReducer,
  50. initialState,
  51. applyMiddleware()
  52. )
  53. }
  54.  
  55.  
  56.  
  57.  
  58.  
  59. // #05 provideing store to all components in index.jsx file (./src/js/index.jsx)
  60. // import 'babel-polyfill'
  61. import React from 'react'
  62. import ReactDOM from 'react-dom'
  63. // redux
  64. import configureStore from './store/configureStore'
  65. import { Provider } from 'react-redux'
  66. // components
  67. import App from './components/App'
  68. // css
  69. import '../scss/app.scss'
  70.  
  71.  
  72. const store = configureStore();
  73.  
  74.  
  75. ReactDOM.render(
  76. <Provider store={store}>
  77. <App />
  78. </Provider>,
  79. document.getElementById('app')
  80. )
  81.  
  82.  
  83.  
  84.  
  85. // #06 connect your component to redux and use it
Add Comment
Please, Sign In to add comment