I'm trying to pass the formatted (JSON) API results into the redux store. I'm able to retrieve the data and resolve the promise but I can't assign the returned data inside the '.then' method.
Can you please tell me what I'm doing wrong? Also I'm newer so please be kind if I mistake a question. Thank you.
Reducer
import apiTest from "../api/ApiTest";
const initialState = {
listData: null
};
const contactListReducer = (state = initialState, action) => {
switch (action.type) {
case "FETCH":
console.log("inside switch (reducer) ");
return getData(state);
default:
return state;
}
};
const getData = (state, action) => {
console.log("Inside getData");
let data;
let request = apiTest().then((results) => {
data = results;
console.log("INSIDE THEN");
console.log(data);
});
console.log("OUTISIDE THEN");
console.log(request);
return Object.assign({}, state, {
listData: request
});
};
export default contactListReducer;
API Code:
import React, { useEffect, useState } from "react";
import axios from "axios";
const apiCall = async () => {
try {
const payload = await fetch("https://jsonplaceholder.typicode.com/users");
const results = await payload.json();
// console.log(results);
return results;
} catch (err) {
console.log(`error found of type ${err}`);
}
};
export default apiCall;