Initialising Mobx store with api call in NextJS
I am following the Nextjs + mobx example but i cannot get the store to get populated by my api call.
It seems to me that the following section of the HOC does not actually wait for the Page's getInitialProps to resolve before continuing with the execution:
if (typeof App.getInitialProps === "function") {
appProps = await App.getInitialProps.call(App, appContext)
}
To be more specific, the execution continues despite the await
statemtent and initialises the store with nothing instead of waiting for the api data before continuing.
I can also tell this because of the various console.log
statements which produce the following output - I can see that the HOC's (and therefore the store) initialisation finishes before the api request is completed in the Page's getInitialProps.
HOC initial props
Page initial props started
HOC initial props complete
HOC constructor
Page initial props complete
Any ideas on what i am doing wrong here?
getInitialProps and export of the index page:
Index.getInitialProps = async function(ctx) {
console.log("Page initial props started")
const r = await ctx.store.postStore.getQuestions()
console.log("Page initial props complete")
return r
}
export default WithAuth(withLayout(withStyles(styles)(Index)))
Store method that call api and populated the store:
getQuestions() {
return apiGetQuestions().then(res => {
// console.log("received data")
return (
res.data.questions.length > 0 &&
(this.posts = res.data.questions.map(
json => new Question(json)
))
)
})
API call function:
export const apiGetQuestions = () => {
return fetch(`${url}/api/questions`, {
headers: {
"Content-Type": "application/json",
mode: "cors",
"Access-Control-Allow-Origin": "http://localhost:3000/"
},
method: "GET"
})
.then(response => response.json())
.catch(e => {
throw "Unable to save record"
})
}
getInitialProps of HOC:
static async getInitialProps(appContext) {
console.log("HOC initial props")
const store = getOrCreateStore()
appContext.ctx.store = store
let appProps = {}
if (typeof App.getInitialProps === "function") {
appProps = await App.getInitialProps.call(App, appContext)
}
console.log("HOC initial props complete")
return {
...appProps,
initialMobxState: store
}
}
async-await next.js mobx-react
add a comment |
I am following the Nextjs + mobx example but i cannot get the store to get populated by my api call.
It seems to me that the following section of the HOC does not actually wait for the Page's getInitialProps to resolve before continuing with the execution:
if (typeof App.getInitialProps === "function") {
appProps = await App.getInitialProps.call(App, appContext)
}
To be more specific, the execution continues despite the await
statemtent and initialises the store with nothing instead of waiting for the api data before continuing.
I can also tell this because of the various console.log
statements which produce the following output - I can see that the HOC's (and therefore the store) initialisation finishes before the api request is completed in the Page's getInitialProps.
HOC initial props
Page initial props started
HOC initial props complete
HOC constructor
Page initial props complete
Any ideas on what i am doing wrong here?
getInitialProps and export of the index page:
Index.getInitialProps = async function(ctx) {
console.log("Page initial props started")
const r = await ctx.store.postStore.getQuestions()
console.log("Page initial props complete")
return r
}
export default WithAuth(withLayout(withStyles(styles)(Index)))
Store method that call api and populated the store:
getQuestions() {
return apiGetQuestions().then(res => {
// console.log("received data")
return (
res.data.questions.length > 0 &&
(this.posts = res.data.questions.map(
json => new Question(json)
))
)
})
API call function:
export const apiGetQuestions = () => {
return fetch(`${url}/api/questions`, {
headers: {
"Content-Type": "application/json",
mode: "cors",
"Access-Control-Allow-Origin": "http://localhost:3000/"
},
method: "GET"
})
.then(response => response.json())
.catch(e => {
throw "Unable to save record"
})
}
getInitialProps of HOC:
static async getInitialProps(appContext) {
console.log("HOC initial props")
const store = getOrCreateStore()
appContext.ctx.store = store
let appProps = {}
if (typeof App.getInitialProps === "function") {
appProps = await App.getInitialProps.call(App, appContext)
}
console.log("HOC initial props complete")
return {
...appProps,
initialMobxState: store
}
}
async-await next.js mobx-react
add a comment |
I am following the Nextjs + mobx example but i cannot get the store to get populated by my api call.
It seems to me that the following section of the HOC does not actually wait for the Page's getInitialProps to resolve before continuing with the execution:
if (typeof App.getInitialProps === "function") {
appProps = await App.getInitialProps.call(App, appContext)
}
To be more specific, the execution continues despite the await
statemtent and initialises the store with nothing instead of waiting for the api data before continuing.
I can also tell this because of the various console.log
statements which produce the following output - I can see that the HOC's (and therefore the store) initialisation finishes before the api request is completed in the Page's getInitialProps.
HOC initial props
Page initial props started
HOC initial props complete
HOC constructor
Page initial props complete
Any ideas on what i am doing wrong here?
getInitialProps and export of the index page:
Index.getInitialProps = async function(ctx) {
console.log("Page initial props started")
const r = await ctx.store.postStore.getQuestions()
console.log("Page initial props complete")
return r
}
export default WithAuth(withLayout(withStyles(styles)(Index)))
Store method that call api and populated the store:
getQuestions() {
return apiGetQuestions().then(res => {
// console.log("received data")
return (
res.data.questions.length > 0 &&
(this.posts = res.data.questions.map(
json => new Question(json)
))
)
})
API call function:
export const apiGetQuestions = () => {
return fetch(`${url}/api/questions`, {
headers: {
"Content-Type": "application/json",
mode: "cors",
"Access-Control-Allow-Origin": "http://localhost:3000/"
},
method: "GET"
})
.then(response => response.json())
.catch(e => {
throw "Unable to save record"
})
}
getInitialProps of HOC:
static async getInitialProps(appContext) {
console.log("HOC initial props")
const store = getOrCreateStore()
appContext.ctx.store = store
let appProps = {}
if (typeof App.getInitialProps === "function") {
appProps = await App.getInitialProps.call(App, appContext)
}
console.log("HOC initial props complete")
return {
...appProps,
initialMobxState: store
}
}
async-await next.js mobx-react
I am following the Nextjs + mobx example but i cannot get the store to get populated by my api call.
It seems to me that the following section of the HOC does not actually wait for the Page's getInitialProps to resolve before continuing with the execution:
if (typeof App.getInitialProps === "function") {
appProps = await App.getInitialProps.call(App, appContext)
}
To be more specific, the execution continues despite the await
statemtent and initialises the store with nothing instead of waiting for the api data before continuing.
I can also tell this because of the various console.log
statements which produce the following output - I can see that the HOC's (and therefore the store) initialisation finishes before the api request is completed in the Page's getInitialProps.
HOC initial props
Page initial props started
HOC initial props complete
HOC constructor
Page initial props complete
Any ideas on what i am doing wrong here?
getInitialProps and export of the index page:
Index.getInitialProps = async function(ctx) {
console.log("Page initial props started")
const r = await ctx.store.postStore.getQuestions()
console.log("Page initial props complete")
return r
}
export default WithAuth(withLayout(withStyles(styles)(Index)))
Store method that call api and populated the store:
getQuestions() {
return apiGetQuestions().then(res => {
// console.log("received data")
return (
res.data.questions.length > 0 &&
(this.posts = res.data.questions.map(
json => new Question(json)
))
)
})
API call function:
export const apiGetQuestions = () => {
return fetch(`${url}/api/questions`, {
headers: {
"Content-Type": "application/json",
mode: "cors",
"Access-Control-Allow-Origin": "http://localhost:3000/"
},
method: "GET"
})
.then(response => response.json())
.catch(e => {
throw "Unable to save record"
})
}
getInitialProps of HOC:
static async getInitialProps(appContext) {
console.log("HOC initial props")
const store = getOrCreateStore()
appContext.ctx.store = store
let appProps = {}
if (typeof App.getInitialProps === "function") {
appProps = await App.getInitialProps.call(App, appContext)
}
console.log("HOC initial props complete")
return {
...appProps,
initialMobxState: store
}
}
async-await next.js mobx-react
async-await next.js mobx-react
edited Nov 26 '18 at 21:40
ThierryMichel
asked Nov 26 '18 at 12:10
ThierryMichelThierryMichel
97113
97113
add a comment |
add a comment |
0
active
oldest
votes
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',
autoActivateHeartbeat: false,
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
});
}
});
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%2f53480846%2finitialising-mobx-store-with-api-call-in-nextjs%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
0
active
oldest
votes
0
active
oldest
votes
active
oldest
votes
active
oldest
votes
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
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%2f53480846%2finitialising-mobx-store-with-api-call-in-nextjs%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