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

320
Vistas
React and Redux: create a variable to use in the project

I'm trying to create, in a project, a controller when the user logged-in.

Basically I would to save some values, not in the db, but in some variables that I can use in the project.

In my idea, this is the actions that the user should do:

  1. The first time that the user logged in, the user will be redirect to a form should choose some values for two variables ( and i want to save these values, that they will be used in to prepoluate some field in the project ) - POST -

  2. The user can change these values - PUT -

  3. Retrieve the data that are saved - GET -

So I'm trying to create a reducer page, to manage these 3 "actions" that he could do.

It's the first time that created a reducers/actions of this type, so:

import { REQUEST, SUCCESS, FAILURE } from 'app/shared/reducers/action-type.util';
export const ACTION_TYPES = {
    FETCH_DATA : 'FETCH_DATA',
    CREATE_DATA : 'CREATE_DATA',
    UPDATE_DATA : 'UPDATE_DATA'
};

export interface PersonInfo {
    info: {
        role?: string | number,
        environment ?: string | number
    }

}




const initialState: PersonInfo =  {
  info: {
      role: '',
      environment : ''
  }
}

// reducers

export default (state: PersonInfo=initialState, action): PersonInfo => {
    switch( action.type ){
        case REQUEST(ACTION_TYPES.FETCH_DATA):
            console.log("FETCH DATA ", state)
            return {
                ...state
            };
        case REQUEST(ACTION_TYPES.CREATE_DATA):
        case REQUEST(ACTION_TYPES.UPDATE_DATA):
        case FAILURE(ACTION_TYPES.FETCH_DATA):
        case FAILURE(ACTION_TYPES.CREATE_DATA):
        case FAILURE(ACTION_TYPES.UPDATE_DATA):
        case SUCCESS(ACTION_TYPES.FETCH_DATA):
            return {
                ...state
            };
        case SUCCESS(ACTION_TYPES.CREATE_DATA):
        case SUCCESS(ACTION_TYPES.UPDATE_DATA): 
            return {
                ...state
            };
        default:
            return state;
    }
}

// now I need to create the actions, and there I have some problems..

// 3. This should be the action to fetch data and to see if the values are populated
export const getData = info =>  {
    return {
        type: ACTION_TYPES.FETCH_DATA,
        payload: info
    }
}

// 1. This should be the action to post data in the variables: (but I don't think it does anything useful .. I should connect my initialState to this function i believe)

  export const createData = entity =>  {
    const userInfo = {
        role: entity.role,
        environment: entity.environment
    }
    const result = ({
      type: ACTION_TYPES.CREATE_DATA,
      payload: userInfo
    });
    console.log("result ", result)
    return result;
  };

So my question is, how can i fix this actions/reducers to work?

Thank you

about 4 years ago · Juan Pablo Isaza
1 Respuestas
Responde la pregunta

0

You need to treat these options just like handling state in a single component, but with the mentality that it's for your global state (hence, Redux). If your tree doesn't branch out to where component A has no way of talking to component B AND you need to access this state in either case, AND you are not changing this state constantly, I would recommend using useContext instead of Redux. This is a fundamental concept within React, and state isn't "RESTful". You are either initializing it, setting it, or reading it.

Take 13 minutes and check out this video on useContext

about 4 years ago · Juan Pablo Isaza 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