Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

124
Views
I have some issue with redux thunk, i am unable to use getState() in my actions file

i have copied my related code down below. please see if you can help me. i am creating a chat application with MERN stack and socket.io .Also i want to know is there any way we can store the chat messages in database. i am thinking it will be too much load on the db server as i have to make a post request everytime a user sends a message.Thank you for your help in advance. This is store.js file

enter coimport { applyMiddleware, createStore, compose } from 'redux'
import rootReducer from './reducers'
import thunk from 'redux-thunk'

const initialState = {};

const composeEnhancers =
  typeof window === 'object' &&
  window.__REDUX_DEVTOOLS_EXTENSION_COMPOSE__ ?   
    window.__REDUX_DEVTOOLS_EXTENSION_COMPOSE__({
    }) : compose;

const enhancer = composeEnhancers(
  applyMiddleware(thunk),
);
const store = createStore(rootReducer,initialState, enhancer);

export default store;

chatActions.js

export const sendMsg=(msg)=> (getState,dispatch)=>{

const socket = getState().chat.socket
socket.emit('message',msg)
.then(dispatch({
    type:'SAVE_MSG',
    payload:msg
}))
}

chatReducer.js

const initialState= {
    msgList:[],
    socket:{},
    msgAreaChat:[]
}

const chatReducer = (state=initialState,action)=>{
    switch(action.type){
        case 'SAVE_MSG':
            return{
                ...state,
                msgList:[...state.msgList,action.payload]

            }
        case 'GET_SOCKET':
            return{
                ...state,
                socket:action.payload
            }
        default:
            return{
                ...state
            }
    }
}

export default chatReducer


The error i am getting is:

about 4 years ago · Juan Pablo Isaza
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!