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

192
Visualizações
React How can add payload in reducer

I wanto add payload in state=> albums =>songs, There are many albums and there will be more than one song in these albums. How can I add the song to the album it belongs to.

state :{ albums : [name, songs:[] ]}

case : "Add_Song"

const reducer = (state,action) =>{
  switch(action.type){
    case "Add_Album": 
    return {
      ...state,
      albums :[...state.albums,action.payload]
    }
    case "Add_Song": 
    return {
      ...state,
      albums : // *********neeed THERE********
    }
    case "Remove_Album" : 
    return {
      ...state,
      albums : state.albums.filter(album =>action.payload.album !== album.album)
    }
    }
    default:
      return state;
  }

}

state :

state = {
    albums: [],
    dispatch : action =>{
      this.setState(state =>reducer(state,action))
    }, 
  };
about 4 years ago · Juan Pablo Isaza
3 Respostas
Responde à pergunta

0

const reducer = (state,action) =>{
  switch(action.type){
    case "Add_Album": 
    return {
      ...state,
      albums :[...state.albums,action.payload]
    }
    case "Add_Song":
    const updatedAlbums = state.albums.map((album) => {
       //use `album.name` to find
       if(album.name === action.payload.album) {
          //modify `songs` list
          return {...album, songs: [...album.songs, action.payload.song]}
       }
       return album // this album does not match, so don't need to change
    })
    return {
      ...state,
      albums : updatedAlbums
    }
    case "Remove_Album" : 
   axios.post("http://localhost:5000/delete",action.payload)
    return {
      ...state,
      albums : state.albums.filter(album =>action.payload.album !== album.album)
    }
    }
    default:
      return state;
  }

}
about 4 years ago · Juan Pablo Isaza Relatório

0

const reducer = (state,action) =>{
  switch(action.type){
    case "Add_Album": 
    return {
      ...state,
      albums :[...state.albums,action.payload]
    }
    case "Add_Song": 
    return {
      ...state,
      albums : albums.map((album) => {
        if(album.id === action.payload.album.id) {
          return {
            ...album,
            songs: [...album.songs, action.payload.song]
          }
        }
        return album;
      })
    }
    case "Remove_Album" : 
    return {
      ...state,
      albums : state.albums.filter(album =>action.payload.album !== album.album)
    }
    }
    default:
      return state;
  }

}
about 4 years ago · Juan Pablo Isaza Relatório

0

You should need to define your initial state like this where albums array contains multiple album in objects formate so we have id,name and songs array in album object like this....

const initialState={
  albums:[
    {id:0,name:'abc',songs:[]}
  ]
}
const reducer=(state=initialState,action)=>{
  switch(action.type){
    case 'Add_Songs':{
      //first get album id, song from actionPayload like this
      const {id,song}=action.payload;
      //find required album by id like ...
      let givenAlbum=state.albums.find(album=>album.id===id);
      //now update given album songs by this
      givenAlbum.songs.push(song);
      //finally return updated albums

      return {
        ...state,
        albums:[
          ...state.albums,givenAlbum
        ]
      }
    }
  }
}
``````````````````````````````````````````````````
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