Skip to content

Instantly share code, notes, and snippets.

@wmucheru
Created January 25, 2018 12:23
Show Gist options
  • Save wmucheru/933b3f5626d95602106dd091d116b7d0 to your computer and use it in GitHub Desktop.
Save wmucheru/933b3f5626d95602106dd091d116b7d0 to your computer and use it in GitHub Desktop.
Accessing Redux Store outside a component
// tooling modules
import axios from 'axios'
// store
import store from '../store'
store.subscribe(listener);
function select(state) {
return state.auth.tokens.authentication_token
}
function listener() {
let token = select(store.getState())
axios.defaults.headers.common['Authorization'] = token;
};
// configuration
const api = axios.create({
baseURL: 'http://localhost:3000/api/v1',
headers: {
'Content-Type': 'application/json',
}
});
export default api
@wmucheru
Copy link
Author

From this answer on StackOverflow: https://stackoverflow.com/a/38480550/3310235

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment