Chinmayee Sahoo
Chinmayee Sahoo

Reputation: 89

store updated value in redux

i got two values i.e.company and id from navigation.

let id = props.route.params.oved.toString();
console.log("id-->",id);
let company = props.route.params.company.toString();
console.log("company--->",company);

i got two values as a integer like this:--

id-->1

comapny-->465

enter image description here

Description of the image:---

if i am giving input 1 in that textInput and click on the card(lets say first card i.e.465 then i am getting those two values in navigation as in interger that i have mention above.so each time i am getting updated values.

i am getting updated values from navigation.

so i want to store those values in redux.

action.js:--

import { CHANGE_SELECTED_COMPANY } from "./action-constants";
export const changeCompany = (updatedCompany, updatedId) => {
  return {
    type: CHANGE_SELECTED_COMPANY,
    updatedCompany,
    updatedId,
  };
};

reducer.js:--

import { CHANGE_SELECTED_COMPANY } from "../actions/action-constants";

const initialState = {
  company: "",
  id: "",
};
const changeCompanyReducer = (state = initialState, action) => {
  switch (action.type) {
    case CHANGE_SELECTED_COMPANY:
      return {
        company: {
          company: action.updatedCompany,
          id: action.updatedId,
        },
      };
    
  }
  return state;
};

export default changeCompanyReducer;

congigure-store.js:--

import changeCompanyReducer from "./reducers/change-company-reducer";
const rootReducer = combineReducers({changeCompanyReducer});

How can i store the update values getting from navigation in Redux?

could you please write code for redux??

Upvotes: 1

Views: 34

Answers (1)

Yitzhak Barzilay
Yitzhak Barzilay

Reputation: 71

First I would recommend writing your action like this:

import { CHANGE_SELECTED_COMPANY } from "./action-constants";
export const changeCompany = (payload) => {
  return {
    type: CHANGE_SELECTED_COMPANY,
    payload // inside payload you can pass: { updatedCompany: '...', updatedId: '...' }
  };
};

And then you need to change your reducer from what you wrote to this:

import { CHANGE_SELECTED_COMPANY } from "../actions/action-constants";

const initialState = {
  company: "",
  id: "",
};
const changeCompanyReducer = (state = initialState, action) => {
  switch (action.type) {
    case CHANGE_SELECTED_COMPANY:
      const { updatedCompany, updatedId } = action.payload;
      
      // notice the changes I made in the return statment.
      return {
        ...state,
        company: updatedCompany,
        id: updatedId
      };
    
    // you can return the state here
    default:
      return state;
  }
};

export default changeCompanyReducer;

Upvotes: 1

Related Questions