How to fetch initial state in redux at first, then I can use it in my component method?
up vote
0
down vote
favorite
I am using react.js with redux
to build my project.
Currently, I store users' token in global state. But how to fetch the token before every component mount? For example. I have an axios action
in redux
to fetch data from REST API in ComponentDidMount
:
Now, this method will goes to catch in ComponenDidMount
becuase it cannot fetch the the token (it is null at first and stored in another reducer).
componentDidMount()
this.props.loadProfile()
// in actions
export const loadProfile = (token) =>
return (dispatch, getState) =>
dispatch(loadProfileStart())
const getUrl = URL + 'myprofile/'
return axios.get(getUrl,
headers:
...HEADERS,
'Authorization': 'Token ' + token // token is in another reducer,
)
.then(res =>
dispatch(loadProfileSuccess(res.data.profile))
return res.data.profile
)
.catch(err =>
dispatch(loadProfileFail(err))
throw (err)
)
I did some research which said I should use middleware
. But I still dont understand the getState because my store is inApp.js
const store = createStore(rootReducer, compostEnhancer(
applyMiddleware(thunk )
))
const app = (
<Provider store=store>
<BrowserRouter>
<App/>
</BrowserRouter>
</Provider>)
Thanks for your help!
reactjs redux
add a comment |
up vote
0
down vote
favorite
I am using react.js with redux
to build my project.
Currently, I store users' token in global state. But how to fetch the token before every component mount? For example. I have an axios action
in redux
to fetch data from REST API in ComponentDidMount
:
Now, this method will goes to catch in ComponenDidMount
becuase it cannot fetch the the token (it is null at first and stored in another reducer).
componentDidMount()
this.props.loadProfile()
// in actions
export const loadProfile = (token) =>
return (dispatch, getState) =>
dispatch(loadProfileStart())
const getUrl = URL + 'myprofile/'
return axios.get(getUrl,
headers:
...HEADERS,
'Authorization': 'Token ' + token // token is in another reducer,
)
.then(res =>
dispatch(loadProfileSuccess(res.data.profile))
return res.data.profile
)
.catch(err =>
dispatch(loadProfileFail(err))
throw (err)
)
I did some research which said I should use middleware
. But I still dont understand the getState because my store is inApp.js
const store = createStore(rootReducer, compostEnhancer(
applyMiddleware(thunk )
))
const app = (
<Provider store=store>
<BrowserRouter>
<App/>
</BrowserRouter>
</Provider>)
Thanks for your help!
reactjs redux
1
Have you looked at this solution? auth0.com/blog/… It's long but it goes in depth on when and where to call the auth api.
– Ortho Home Defense
Nov 9 at 23:03
add a comment |
up vote
0
down vote
favorite
up vote
0
down vote
favorite
I am using react.js with redux
to build my project.
Currently, I store users' token in global state. But how to fetch the token before every component mount? For example. I have an axios action
in redux
to fetch data from REST API in ComponentDidMount
:
Now, this method will goes to catch in ComponenDidMount
becuase it cannot fetch the the token (it is null at first and stored in another reducer).
componentDidMount()
this.props.loadProfile()
// in actions
export const loadProfile = (token) =>
return (dispatch, getState) =>
dispatch(loadProfileStart())
const getUrl = URL + 'myprofile/'
return axios.get(getUrl,
headers:
...HEADERS,
'Authorization': 'Token ' + token // token is in another reducer,
)
.then(res =>
dispatch(loadProfileSuccess(res.data.profile))
return res.data.profile
)
.catch(err =>
dispatch(loadProfileFail(err))
throw (err)
)
I did some research which said I should use middleware
. But I still dont understand the getState because my store is inApp.js
const store = createStore(rootReducer, compostEnhancer(
applyMiddleware(thunk )
))
const app = (
<Provider store=store>
<BrowserRouter>
<App/>
</BrowserRouter>
</Provider>)
Thanks for your help!
reactjs redux
I am using react.js with redux
to build my project.
Currently, I store users' token in global state. But how to fetch the token before every component mount? For example. I have an axios action
in redux
to fetch data from REST API in ComponentDidMount
:
Now, this method will goes to catch in ComponenDidMount
becuase it cannot fetch the the token (it is null at first and stored in another reducer).
componentDidMount()
this.props.loadProfile()
// in actions
export const loadProfile = (token) =>
return (dispatch, getState) =>
dispatch(loadProfileStart())
const getUrl = URL + 'myprofile/'
return axios.get(getUrl,
headers:
...HEADERS,
'Authorization': 'Token ' + token // token is in another reducer,
)
.then(res =>
dispatch(loadProfileSuccess(res.data.profile))
return res.data.profile
)
.catch(err =>
dispatch(loadProfileFail(err))
throw (err)
)
I did some research which said I should use middleware
. But I still dont understand the getState because my store is inApp.js
const store = createStore(rootReducer, compostEnhancer(
applyMiddleware(thunk )
))
const app = (
<Provider store=store>
<BrowserRouter>
<App/>
</BrowserRouter>
</Provider>)
Thanks for your help!
reactjs redux
reactjs redux
edited Nov 9 at 22:51
asked Nov 9 at 22:39
Chao
2516
2516
1
Have you looked at this solution? auth0.com/blog/… It's long but it goes in depth on when and where to call the auth api.
– Ortho Home Defense
Nov 9 at 23:03
add a comment |
1
Have you looked at this solution? auth0.com/blog/… It's long but it goes in depth on when and where to call the auth api.
– Ortho Home Defense
Nov 9 at 23:03
1
1
Have you looked at this solution? auth0.com/blog/… It's long but it goes in depth on when and where to call the auth api.
– Ortho Home Defense
Nov 9 at 23:03
Have you looked at this solution? auth0.com/blog/… It's long but it goes in depth on when and where to call the auth api.
– Ortho Home Defense
Nov 9 at 23:03
add a comment |
1 Answer
1
active
oldest
votes
up vote
0
down vote
in my project if test in App constructor if user is logged in. For that, i control in my redux state my variable isLoggedIn. I use mapStateToProps for this.
If isLoggedIn i get token from API and i save him in localstorage then i show home componant else is !isLoggedIn i show loggin componant.
I hope this way help you a little.
1
Previously, I was doing that with localstorage. But the others told me that it is not safe to store the token in the localstorage. Is it true?
– Chao
Nov 9 at 23:37
1
Yes i store token in localstorage because we do not need a lot of security for our internal software. The best way is here : auth0.com/docs/security/store-tokens . Eventually I will change my place of storage to put it in a cookie.
– A.Vincent
Nov 9 at 23:43
add a comment |
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
0
down vote
in my project if test in App constructor if user is logged in. For that, i control in my redux state my variable isLoggedIn. I use mapStateToProps for this.
If isLoggedIn i get token from API and i save him in localstorage then i show home componant else is !isLoggedIn i show loggin componant.
I hope this way help you a little.
1
Previously, I was doing that with localstorage. But the others told me that it is not safe to store the token in the localstorage. Is it true?
– Chao
Nov 9 at 23:37
1
Yes i store token in localstorage because we do not need a lot of security for our internal software. The best way is here : auth0.com/docs/security/store-tokens . Eventually I will change my place of storage to put it in a cookie.
– A.Vincent
Nov 9 at 23:43
add a comment |
up vote
0
down vote
in my project if test in App constructor if user is logged in. For that, i control in my redux state my variable isLoggedIn. I use mapStateToProps for this.
If isLoggedIn i get token from API and i save him in localstorage then i show home componant else is !isLoggedIn i show loggin componant.
I hope this way help you a little.
1
Previously, I was doing that with localstorage. But the others told me that it is not safe to store the token in the localstorage. Is it true?
– Chao
Nov 9 at 23:37
1
Yes i store token in localstorage because we do not need a lot of security for our internal software. The best way is here : auth0.com/docs/security/store-tokens . Eventually I will change my place of storage to put it in a cookie.
– A.Vincent
Nov 9 at 23:43
add a comment |
up vote
0
down vote
up vote
0
down vote
in my project if test in App constructor if user is logged in. For that, i control in my redux state my variable isLoggedIn. I use mapStateToProps for this.
If isLoggedIn i get token from API and i save him in localstorage then i show home componant else is !isLoggedIn i show loggin componant.
I hope this way help you a little.
in my project if test in App constructor if user is logged in. For that, i control in my redux state my variable isLoggedIn. I use mapStateToProps for this.
If isLoggedIn i get token from API and i save him in localstorage then i show home componant else is !isLoggedIn i show loggin componant.
I hope this way help you a little.
answered Nov 9 at 23:27
A.Vincent
367
367
1
Previously, I was doing that with localstorage. But the others told me that it is not safe to store the token in the localstorage. Is it true?
– Chao
Nov 9 at 23:37
1
Yes i store token in localstorage because we do not need a lot of security for our internal software. The best way is here : auth0.com/docs/security/store-tokens . Eventually I will change my place of storage to put it in a cookie.
– A.Vincent
Nov 9 at 23:43
add a comment |
1
Previously, I was doing that with localstorage. But the others told me that it is not safe to store the token in the localstorage. Is it true?
– Chao
Nov 9 at 23:37
1
Yes i store token in localstorage because we do not need a lot of security for our internal software. The best way is here : auth0.com/docs/security/store-tokens . Eventually I will change my place of storage to put it in a cookie.
– A.Vincent
Nov 9 at 23:43
1
1
Previously, I was doing that with localstorage. But the others told me that it is not safe to store the token in the localstorage. Is it true?
– Chao
Nov 9 at 23:37
Previously, I was doing that with localstorage. But the others told me that it is not safe to store the token in the localstorage. Is it true?
– Chao
Nov 9 at 23:37
1
1
Yes i store token in localstorage because we do not need a lot of security for our internal software. The best way is here : auth0.com/docs/security/store-tokens . Eventually I will change my place of storage to put it in a cookie.
– A.Vincent
Nov 9 at 23:43
Yes i store token in localstorage because we do not need a lot of security for our internal software. The best way is here : auth0.com/docs/security/store-tokens . Eventually I will change my place of storage to put it in a cookie.
– A.Vincent
Nov 9 at 23:43
add a comment |
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53234166%2fhow-to-fetch-initial-state-in-redux-at-first-then-i-can-use-it-in-my-component%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
1
Have you looked at this solution? auth0.com/blog/… It's long but it goes in depth on when and where to call the auth api.
– Ortho Home Defense
Nov 9 at 23:03