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

313
Views
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 answers
Answer question

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 Report
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!