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

300
Views
How do I connect to my state by ID params in Redux Toolkit when the data is an object not an array?

Take a look at my Slice below

import { createSlice, createAsyncThunk } from "@reduxjs/toolkit";
import axios from "axios";

const KEY = process.env.REACT_APP_API_KEY
const BASE_URL = process.env.REACT_APP_BASE_URL
const API = `${BASE_URL}/countrymap`

const initialState = {
  mapdata:[],
  status: 'idle',  
  error:null
}

export const fetchMapData = createAsyncThunk(
  'mapdata/fetchMapData', 
  async (id) => { 
    try {
      const response = await axios.get(
        API,
        {
          headers: {
            'Content-Type': 'application/json',
            'X-API-KEY': KEY,
          },
          params: {
            titleId: id,
          }
        }
      )

      return response.data.Item;
    } catch (error) {
      console.error('API call error:', error.message);
    }
  }
)

const mapSlice = createSlice({
  name: 'mapdata',
  initialState,
  reducers:{
  },
  extraReducers(builder) {
    builder
      .addCase(fetchMapData.fulfilled, (state, action) => {
        state.status = 'succeeded'
        //This attempt to make it an array failed with the error
        //  that the state is not iterable
        state.mapdata = [action.payload, ...state] 
        console.log("map payload: ", state.mapdata);
      })
  }
})

// SELECTORS
// This works for an array, but the data is originally coming 
//  in as an object instead
export const allMapData = (state, id) => state.mapdata.mapdata.find(item => item.title_id === id); 

export default mapSlice.reducer

for reference, look at these two console logs from two different API calls to two different endpoints from two different slices . Except Media is coming back as an array, and map is an object

enter image description here

I need to either, turn the state.mapdata into an Object to I can use the selector the way it is or re-code the selector so that it doesn't use the .find() function because that's an array function. But either way, it needs to compare the titleId in the data to the id in the params

Sorry for not providing workable code. I would but there is an insane amount of dependencies here

about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

You should use

state.mapdata = [action.payload, ...state.mapdata] 

instead of

state.mapdata = [action.payload, ...state] 
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!