Guest User

Untitled

a guest
Jan 23rd, 2019
85
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.39 KB | None | 0 0
  1. import React from 'react';
  2. import { connect } from "react-redux";
  3.  
  4. class NotesBody extends React.Component {
  5. render(){
  6. return(
  7. <ul>
  8. {
  9. this.props.notes.map((note) => {
  10. return <li key={note.id}>{note.noteContent}</li>
  11. })
  12. }
  13. </ul>
  14. );
  15. }
  16. }
  17.  
  18. function mapStateToProps(state){
  19. console.log("[STATE NOTESBODY]", state);
  20.  
  21. return {
  22. notes: state.notes
  23. }
  24. }
  25.  
  26. export default connect(mapStateToProps)(NotesBody);
  27.  
  28. import { createStore } from 'redux';
  29. import { app } from '../config/config';
  30.  
  31. const initialState = {
  32. database: app.database().ref().child('notities'),
  33. notes: []
  34. }
  35.  
  36. const reducer = (state = initialState, action) => {
  37. console.log("Reducer running", action.type);
  38.  
  39. switch(action.type){
  40. default:
  41. break;
  42.  
  43. case "DB_NOTE_ADDED":
  44. console.log("DB_NOTE_ADDED", action.payload.notes);
  45. state = {
  46. ...state,
  47. notes: action.payload.notes
  48. }
  49. break;
  50. }
  51.  
  52. return state;
  53. }
  54.  
  55. const store = createStore(reducer);
  56.  
  57. store.subscribe(() => {
  58. console.log("Store updated", store.getState());
  59. });
  60.  
  61. export default store;
  62.  
  63. var previousNotes = [
  64. { id: "1", noteContent: "Hoi" }
  65. ];
  66. store.dispatch({type: "DB_NOTE_ADDED", payload: { notes: previousNotes }})
Add Comment
Please, Sign In to add comment