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

144
Visualizações
How to access initial state array of object from reducer

I'm facing a problem accessing initialstates array of object for redux.

I have this initial states:

 export const initialState = [
  {
    blogPosts: [
      {
        title: "",
        category: "",
        logo: "",
        author: "",
        date: "",
        timeToRead: "",
        blogImage: "",
      },
    ],
  },
  { filterStatus: "" },
  { filterBy: "" },
];

and I only want to change the filterStatus from my reducer. But trying to do so its changing and also adding one more object in the array.

I was trying this code from my reducer:

case SEARCH_AUTHOR:
      return [
        ...state,   
        (state[1].filterStatus = "author"),

      ];

How can I change get rid of this problem?

about 4 years ago · Juan Pablo Isaza
3 Respostas
Responde à pergunta

0

What you are doing here:

case SEARCH_AUTHOR:
  return [
    ...state,   
    (state[1].filterStatus = "author"),
];

Is creating a new object with the entirety of state - (...state) And then modifying the 2nd item's filter status, but only returning the result of the expression which is the newly set value "author".

There are many ways to implement what you wanted, I think the simplest would be to copy the array then modify the copy before returning.

case SEARCH_AUTHOR:
  const copy = [...state]
  copy[1] = {...copy[1], filterStatus: 'author'}
  return copy;

What this does is:

  1. Create a shallow copy of the array - this copies the same object references to a new array
  2. Overwrite item at index 1 with a new object that spreads (copies) the original and then overwrites the required key

Another possible solution would be to use map or reduce on the array and returning the modified new array, or splice a new object at the correct index

about 4 years ago · Juan Pablo Isaza Relatório

0

You can use splice state.splice(1, 1, { filterStatus: "author" } to remove the { filterStatus: "" } and set instead { filterStatus: "author" } and return a copy of the state by return [...state].

const initialState = [
    {
      blogPosts: [
        {
          title: "",
          category: "",
          logo: "",
          author: "",
          date: "",
          timeToRead: "",
          blogImage: "",
        },
      ],
    },
    { filterStatus: "" },
    { filterBy: "" },
  ];
  
  const changeFilter = () => {
    initialState.splice(1, 1, { filterStatus: "author" } )
    return [ ...initialState ];
  }
  
  console.log(changeFilter())

about 4 years ago · Juan Pablo Isaza Relatório

0

Use var instead of const

 export var initialState = [
 {
  blogPosts: [
    {
    title: "",
    category: "",
    logo: "",
    author: "",
    date: "",
    timeToRead: "",
    blogImage: "",
    },
    ],
  },
    { filterStatus: "" },
    { filterBy: "" },
 ];
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