Hi I created a redux custom middleware and trying to call apiUrl contstant value defined and exported from ./utils/api.js file. but getting this error
ReferenceError: Cannot access 'apiUrl' before initialization
.utils/api.jsfile
export const apiUrl = window.apiUrl || `${window.location.origin}/api`;
redux-custom-middleware.jsfile
import * as api from './config/api';
const apiUrl = api.apiUrl;
Though, this constant value is accessible in the entire app (all components and actions) but unable to access it inside redux custom middleware.
Thanks
You can export a function instead of a constant, as functions are eagerly declared:
This makes it you can use
apiUrl()immediately, since the circular dependency is fine since functions are "initialized" instantly.