1.0.5 • Published 5 years ago

@enqode/ts-reducer v1.0.5

Weekly downloads
-
License
MIT
Repository
github
Last release
5 years ago

Usage

import { createStore, AnyAction, Dispatch } from 'redux'
import { create } from './../src/index'

/**
 * Defining the state
 */

type Message = {
  username: string
  message: string
}

type ChatState = {
  messages: Message[]
}

const initialState: ChatState = {
  messages: []
}


/**
 * Creating the reducer
 */

type SendMessage = {
  username: string
  message: string
}

const sendMessage = create<ChatState, SendMessage>('SEND_MESSAGE', (state, message) => {
  return{
    ...state,
    messages:[
      ...state.messages,
      message
    ]
  }
})


/**
 * Using the reducer inside your rootReducer
 */

const reducersMap = {
  [sendMessage.type]: sendMessage.reducer
}

const rootReducer = (state: ChatState = initialState, action: AnyAction) => {
  if(reducersMap[action.type]){
    return reducersMap[action.type](state, action.payload || null)
  }
  return state
}

const store = createStore(rootReducer)


/**
 * How to dispatch actions
 */

// using store.dispatch directly
sendMessage.createDispatch(store.dispatch)({
  username: 'enqode',
  message: 'Hey there!',
})

// using react-redux's connect function
const mapDispatchToProps = (dispatch: Dispatch) => ({
  sendMessage: (message: string) => sendMessage.createDispatch(dispatch)({
    username: 'enqode',
    message
  })
})
1.0.5

5 years ago

1.0.4

5 years ago

1.0.3

5 years ago

1.0.2

5 years ago

1.0.1

5 years ago

1.0.0

5 years ago