Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

141
Vistas
Redux-toolkit + Signalr

What is the best way to update the state with signalr? At this moment I dispatching data inside on method callback and accessing it using useSelector inside other component. I wonder if this is the optimal approach.

useEffect(()=> {

 const connection = $.hubConnection("http://x.x.x.x/signalr", { useDefaultPath: false })
 const proxy = connection.createHubProxy("exampleHub")

 proxy.on("action", function(data) {
   dispatch(setStatus(data))
 }
})

Slice.js:

export const monitorSlice = createSlice({
  name: "example",
  initialState: { 
    status: {},
},
  reducers: {
    setStatus(state, {payload}) => {
      state.status = payload
    }
  }
});
about 4 years ago · Santiago Trujillo
1 Respuestas
Responde la pregunta

0

Your approach will work, Here is other approach that you can have:

Using your own middleware

Another approach that is more agnostic is to use a middleware

import { Middleware } from 'redux'
import { monitorActions } from './monitorSlice';
 
const monitorMiddleware: Middleware = store => next => action => {
  if (!monitorActions.startConnecting.match(action)) {
    return next(action);
  }

  const connection = $.hubConnection("http://x.x.x.x/signalr", { useDefaultPath: false })
  const proxy = connection.createHubProxy("exampleHub")

  proxy.on("action", function(data) {
    dispatch(setStatus(data))
  });
 
  next(action);
}
 
export default monitorMiddleware;

Then inside your react app when you want to establish the connection

useEffect(()=> {
   dispatch(monitorActions.startConnecting())
}, [])

Then when you setup your store, do not forget to add your new middleware monitorMiddleware:

export const store = configureStore({
  reducer: rootReducer,
  middleware: (getDefaultMiddleware) => {
    return getDefaultMiddleware().concat([monitorMiddleware])
  },
});

Using create api

Another approach is to use createApi.

about 4 years ago · Santiago Trujillo Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda