Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

143
Visualizações
Recoil like implementation in Redux

I'm trying to setup Redux to replace recoil and would like to keep the implementation style of recoil. One where you can do something like

A) setState({ name: 'tim', id: 5 })

or

B) setState(({name}: User) => ({ name, id: 5 }));

I tried with

type PropertyFunction<T> = (newState: Partial<T>) => T;
type StateSetter<S> = Partial<S> | PropertyFunction<S>;

export const stateSetter = <T>(dispatch: Dispatch, setAction: ActionCreatorWithPayload<Partial<T>>, state: T) => {
      return (newState: StateSetter<T>) => {
        if (typeof newState === 'function') {
          dispatch(setAction(newState(state)));
        } else {
          dispatch(setAction(newState));
        }
      };
    };

But the stateSetter function does not accept functions, only (a user)

Meaning I can only do A) but not B)

Here is my simple setState reducer

export const alertSlice = createSlice({
  name: 'user',
  initialState,
  reducers: {
    set: (state: AlertState, action: PayloadAction<Partial<AlertState>>) => {
      state = { ...state, ...action.payload };
    },
  },
});
export const { set: setAlertAction } = alertSlice.actions;
about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

Can you use other alternatives like MobX, Jotai or Valtio?

Redux is really not meant to be used like this - actions should describe events happening in your application and the logic calculating the next state should live in the reducer.

If you want the logic outside the store, you will be off much better with another library and it will feel a lot "less weird" to use.

about 4 years ago · Juan Pablo Isaza Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda