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!










share|improve this question



















  • 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














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!










share|improve this question



















  • 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












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!










share|improve this question















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






share|improve this question















share|improve this question













share|improve this question




share|improve this question








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












  • 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












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.






share|improve this answer
















  • 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











Your Answer






StackExchange.ifUsing("editor", function ()
StackExchange.using("externalEditor", function ()
StackExchange.using("snippets", function ()
StackExchange.snippets.init();
);
);
, "code-snippets");

StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "1"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);

else
createEditor();

);

function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);



);













 

draft saved


draft discarded


















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

























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.






share|improve this answer
















  • 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















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.






share|improve this answer
















  • 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













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.






share|improve this answer












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.







share|improve this answer












share|improve this answer



share|improve this answer










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













  • 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


















 

draft saved


draft discarded















































 


draft saved


draft discarded














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





















































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







Popular posts from this blog

Darth Vader #20

How to how show current date and time by default on contact form 7 in WordPress without taking input from user in datetimepicker

Ondo