Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import { createStore } from 'redux';
- // Reducer
- const initialState = {
- switch: false,
- name: ''
- }
- function reducer (state = initialState, action = {}) {
- switch (action.type) {
- case 'flip':
- return {
- ...state,
- switch: !state.switch,
- name: action.payload.name
- }
- default:
- return state
- }
- }
- // Store
- let store = createStore(reducer);
- store.getState() // { switch: false }
- const myaction = {
- type: 'flip',
- payload: {
- name: 'kieran'
- }
- }
- // Action creator
- function flipSwitch(name) {
- return {
- type: 'flip',
- payload: {
- name
- }
- }
- }
- store.dispatch(flipSwitch('kieran')) // with action creator
- store.dispatch(myaction) // Normal action
Add Comment
Please, Sign In to add comment