Created
January 25, 2018 12:23
-
-
Save wmucheru/933b3f5626d95602106dd091d116b7d0 to your computer and use it in GitHub Desktop.
Accessing Redux Store outside a component
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
From this answer on StackOverflow: https://stackoverflow.com/a/38480550/3310235