how to get use getstate or how to get astrology slice state in thunk

16 Views Asked by At

i want to access data (un) from slice to createasyncthunk using getstate or any other way possible ??

const astrology =  createSlice({
    name:'astroreport',
    initialState: {
        isloading:false,
        adata: null,
        isError:false,
        un:'saadsd',
    },
    extraReducers: (builder) => {
        builder.addCase(fetchastro.pending,(state, action)=>{
            console.log(action.payload)
            state.isloading = true;
        })
        builder.addCase(fetchastro.fulfilled,(state, action)=>{  
            console.log(action.payload)
            state.isloading = false
            state.adata = action.payload
         })
         builder.addCase(fetchastro.rejected,(state,action) => {
            console.log("rejected",action.error.message)
            state.isError = true;
         })
    }
})
export const fetchastro = createAsyncThunk('atechs',async(thunkAPI)=>{
    console.log(thunkAPI.getState().astrology)
    const response = await fetch("https://json.astrologyapi.com/v1/"+api, {
        method: 'POST',
        headers: {
          Authorization: auth,
          'Content-Type': 'application/json',
        },    body: JSON.stringify(data)
        });
        
        return response.json(); 
})

export default astrology.reducer

error astro.js:38 rejected Cannot read properties of undefined (reading 'getState')

0

There are 0 best solutions below