Advertisement
Guest User

Untitled

a guest
Oct 15th, 2019
111
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.73 KB | None | 0 0
  1. // Step 1:
  2. // Create an actions and a reducers folder. Then add an index.js file in both folders.
  3.  
  4. ************************************************
  5.  
  6. // Step 2:
  7. // @ Root of application create a <config>.js file.
  8.  
  9. import { createStore } from 'redux';
  10. import reducer from './src/reducers';
  11.  
  12. const configRedux = () => createStore(reducer);
  13.  
  14. export default configRedux;
  15.  
  16. ************************************************
  17.  
  18. // Step 3:
  19. // Import configRedux() into index.js and wrap <App />
  20.  
  21. import configRedux from '../configRedux';
  22. import { Provider } from 'react-redux';
  23.  
  24. const store = configRedux();
  25.  
  26. // Here you'll wrap App in the ReactDOM render
  27. <Provider store={store}> <App/> </Provider>
  28.  
  29. // Don't worry... The app is freaking out because we haven't built the reducer yet.
  30.  
  31. ************************************************
  32.  
  33. // Step 4
  34. // @/actions/index.js
  35. // You'll want to export const any actions then wrap that variable into a function.
  36.  
  37. export const ACTION_NAME = "ACTION_NAME";
  38.  
  39. // This is known as the action creator.
  40. export const actionName = payload => ({
  41. type: ACTION_NAME,
  42. payload: payload
  43. });
  44.  
  45.  
  46. ************************************************
  47.  
  48. // Step 5
  49. // @/reducers/newReducer
  50. // Create a new reducer and import actions from appropriate action file; in this case it's index.js
  51.  
  52. import { ACTION_NAME } from "../actions/index.js";
  53.  
  54. const initialState = {
  55. date: Date.now()
  56. };
  57.  
  58. const newReducer = (state = initialState, action) => {
  59. switch (action.type) {
  60. case ACTION_NAME:
  61. return {
  62. ...state,
  63. date: action.payload
  64. };
  65. default:
  66. return state;
  67. }
  68. };
  69.  
  70. export default newReducer;
  71. // Now that it's exported, import it into @/reducers/index.js. Add to the combineReducers function in the export default.
  72.  
  73.  
  74. ************************************************
  75.  
  76. // Step 6
  77. // @/reducers/index.js
  78.  
  79. import { combineReducers } from 'redux';
  80. import newReducer from '../reducers/newReducer';
  81. // You'll also need to import any other necessary custom reducers.
  82.  
  83. export default combineReducers({
  84. newReducer
  85. // Include any other reducers required by your application.
  86. });
  87.  
  88.  
  89. ************************************************
  90.  
  91. // Step 7
  92. // How to consume the data using hooks within a functional component.
  93.  
  94. // ! RFCE --> Tab for a new functional component if you're using ES7 extension in VS Code.
  95. import React from "react";
  96. import { useDispatch, useSelector } from "react-redux";
  97. import { actionName } from "../actions/index.js";
  98.  
  99. const NewComponent = props => {
  100. const store = useSelector(state => state.newReducer);
  101. const dispatch = useDispatch();
  102.  
  103. return (
  104. <div>
  105. <span>{store.date}</span>
  106. <button onClick={() => dispatch(actionName(`${Date.now()}`))}>
  107. Change Date
  108. </button>
  109. </div>
  110. );
  111. };
  112.  
  113. export default NewComponent;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement