Answer the question
In order to leave comments, you need to log in
How to make export to react\redux after API request?
Hello.
I am writing an application on react \ redux and I have such a reducer
import axios from 'axios';
let initialState = [];
axios.defaults.headers['Authorization'] = `Bearer ` + sessionStorage.getItem('accessToken');
axios.get('/webchat/index')
.then((res) => {
console.log(res.data);
initialState = res.data;
}
)
.catch(
(err) => {
console.log(err.response);
}
);
console.log(initialState);
export default function(initialState, action) {
return initialState;
}
Answer the question
In order to leave comments, you need to log in
The very question is posed against the concept of Redux , and indeed the very modular concept of JavaScript . You don't have to try to export the module after the request, you can't do that. You need to set the initial state and define the isLoading , isError state keys there . When the application is initialized, a request will be made to the server for the data, and until they arrive, isLoading will be set to true , you, ideally, should show the user a preloader at this moment.
Well, what you tried to do is fundamentally wrong.
When using redux-thunk , you should get something like:
import { fetchInitialDataApi } from './api';
const FETCH_INITIAL_DATA_REQUEST = 'FETCH_INITIAL_DATA_REQUEST';
const FETCH_INITIAL_DATA_SUCCESS = 'FETCH_INITIAL_DATA_SUCCESS';
const FETCH_INITIAL_DATA_ERROR = 'FETCH_INITIAL_DATA_ERROR';
const fetchInitialDataRequest = () => ({ type: FETCH_INITIAL_DATA_REQUEST });
const fetchInitialDataSuccess = data => ({
type: FETCH_INITIAL_DATA_SUCCES,
payload: data,
});
const fetchInitialDataError = error => ({
type: FETCH_INITIAL_DATA_ERROR,
payload: error,
});
const fetchInitialData => async dispatch => {
try {
dispatch(fetchInitialDataRequest());
const { data } = await fetchInitialDataApi();
dispatch(fetchInitialDataSuccess(data));
} catch error {
dispatch(fetchInitialDataError(error));
}
};
const initialState = {
data: {},
isLoading: false,
isError: false,
error: null,
};
export default function(state = initialState, action) {
const { type, payload } = action;
switch (type) {
case FETCH_INITIAL_DATA_REQUEST:
return {
...state,
isLoading: true,
isError: false,
error: null,
};
case FETCH_INITIAL_DATA_SUCCESS:
return {
...state,
data: payload,
isLoading: false,
};
case FETCH_INITIAL_DATA_ERROR:
return {
...state,
isLoading: false,
isError: true,
error: payload,
};
default:
return state;
}
}
export default (state = window._data) => state;
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question