Passing data between pages received via fetch in getInitialProps of a page componentWhat is the difference between state and props in React?Why use Redux over Facebook Flux?Why do we need middleware for async flow in Redux?next.js getInitialProps in a global layout componentNot able to return data from getInitialProps() of lower order page componentgetInitialProps is not called when visiting directly the page url in nextjsNextjs and Context APInext.js pass props from HOC to child components getInitialPropsWhy can't I fetch my Metamask account address with Next.js's getInitialProps?Next.JS — getInitialProps to pass props and state to child components via ContextProvider
Subaru automotive wiring and fuses - 0.3mm2 wires with 15A fuses?
Round away from zero
Was "The Hobbit" ever abridged?
Is it rude to ask my opponent to resign an online game when they have a lost endgame?
Where on Earth is it easiest to survive in the wilderness?
Is there any difference between these two sentences? (Adverbs)
Professor refuses to write a recommendation letter
Is it possible to observe space debris with Binoculars?
Bidirectional Dictionary
Why there are construction cranes on apparently completed buildings in New York?
Fantasy Military Arms and Armor: the Dwarven Grand Armory
What's the difference between a share and a stock?
How were the names on the memorial stones in Avengers: Endgame chosen, out-of-universe?
Go for an isolated pawn
Why do we need explainable AI?
Can a readied action be taken after a spell is cast, but before it deals damage?
Numerical minimum of a one-valued function
What is the statistical difference between "choose either total" or "choose new total" when rerolling damage die?
Count rook moves 1D
Why would a hard-tail guitar need a locking nut?
What are some countries where you can be imprisoned for reading or owning a Bible?
MOSFET broke after attaching capacitor bank
What drugs were used in England during the High Middle Ages?
If I have an accident, should I file a claim with my car insurance company?
Passing data between pages received via fetch in getInitialProps of a page component
What is the difference between state and props in React?Why use Redux over Facebook Flux?Why do we need middleware for async flow in Redux?next.js getInitialProps in a global layout componentNot able to return data from getInitialProps() of lower order page componentgetInitialProps is not called when visiting directly the page url in nextjsNextjs and Context APInext.js pass props from HOC to child components getInitialPropsWhy can't I fetch my Metamask account address with Next.js's getInitialProps?Next.JS — getInitialProps to pass props and state to child components via ContextProvider
.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty margin-bottom:0;
Using Next.js
How do I pass some data received by a fetch in getInitialProps() of a page component (say page A) to another page component (say page B)? I do not want the fetch to happen again when the user navigates from A to the second page (page B).
Tried an approach using React Context, where data from page component is sent to context provider, which can be accessed by page B. But the problem is if user directly navigates to B, it doesn't have the data as it is fetched in getInitialProps() of A
reactjs next.js
add a comment |
Using Next.js
How do I pass some data received by a fetch in getInitialProps() of a page component (say page A) to another page component (say page B)? I do not want the fetch to happen again when the user navigates from A to the second page (page B).
Tried an approach using React Context, where data from page component is sent to context provider, which can be accessed by page B. But the problem is if user directly navigates to B, it doesn't have the data as it is fetched in getInitialProps() of A
reactjs next.js
are you using react-router v4?
– Avinash Mahlawat
Mar 28 at 4:03
Sorry should have been clearer. Using Next.js
– Smarajit
Mar 28 at 4:04
add a comment |
Using Next.js
How do I pass some data received by a fetch in getInitialProps() of a page component (say page A) to another page component (say page B)? I do not want the fetch to happen again when the user navigates from A to the second page (page B).
Tried an approach using React Context, where data from page component is sent to context provider, which can be accessed by page B. But the problem is if user directly navigates to B, it doesn't have the data as it is fetched in getInitialProps() of A
reactjs next.js
Using Next.js
How do I pass some data received by a fetch in getInitialProps() of a page component (say page A) to another page component (say page B)? I do not want the fetch to happen again when the user navigates from A to the second page (page B).
Tried an approach using React Context, where data from page component is sent to context provider, which can be accessed by page B. But the problem is if user directly navigates to B, it doesn't have the data as it is fetched in getInitialProps() of A
reactjs next.js
reactjs next.js
edited Mar 28 at 4:22
Smarajit
asked Mar 28 at 3:50
SmarajitSmarajit
491 silver badge8 bronze badges
491 silver badge8 bronze badges
are you using react-router v4?
– Avinash Mahlawat
Mar 28 at 4:03
Sorry should have been clearer. Using Next.js
– Smarajit
Mar 28 at 4:04
add a comment |
are you using react-router v4?
– Avinash Mahlawat
Mar 28 at 4:03
Sorry should have been clearer. Using Next.js
– Smarajit
Mar 28 at 4:04
are you using react-router v4?
– Avinash Mahlawat
Mar 28 at 4:03
are you using react-router v4?
– Avinash Mahlawat
Mar 28 at 4:03
Sorry should have been clearer. Using Next.js
– Smarajit
Mar 28 at 4:04
Sorry should have been clearer. Using Next.js
– Smarajit
Mar 28 at 4:04
add a comment |
2 Answers
2
active
oldest
votes
You can setup Redux with your NextJS setup and check in the getInitialProps() function of B if the store has the respective data using store.getState() and accordingly perform a fetch. This will also help you maintain the data in case of navigation from A to B and then performing a page refresh on B.
Setting up Redux can be a little tricky but this article basically sums it up.
add a comment |
Adding to what Harshail Shah, you have the option of setting up Redux to allow for getInitialProps to read the data. But in addition to that, you have a lot of other options that you can try. Each of them have their advantages and disavantages, usually determined by how much data you are moving around.
1. Trying a pseudo-flash approach
In this approach, you temporarily store data in one getInitialProps using cookies and in the next page, you clear the cookie. One package you can use which automatically picks the cookie (whether client-side or server-side) is next-cookie So, your approach will be:
class Component1 extends React.Component
static async getInitialProps(ctx)
const isServer = !!ctx.req;
// Set the cookie here. You can use next-cookie or ctx.req.cookie
and then in the second component, you will have:
class Component2 extends React.Component
static async getInitialProps(ctx)
const cookies = NextCookie(ctx);
const desiredCookie = cookies['key_name'];
// You can then delete the cookie here so that the data won't be available the next time
2. Encrypting the data and passing it as a query parameter to the next page
In my opinion, this is responsible for a lot of long URLs we see around. Basically the idea is on click of a button, you convert the data either into a base64 string or use a very fast browser encrypter/decrypter to append the data to the new URL as a query parameter and read it in the next page.
The best approach will usually depend on how much data you are encrypting; and how long you want the data to persist.
add a comment |
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%2f55389893%2fpassing-data-between-pages-received-via-fetch-in-getinitialprops-of-a-page-compo%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
You can setup Redux with your NextJS setup and check in the getInitialProps() function of B if the store has the respective data using store.getState() and accordingly perform a fetch. This will also help you maintain the data in case of navigation from A to B and then performing a page refresh on B.
Setting up Redux can be a little tricky but this article basically sums it up.
add a comment |
You can setup Redux with your NextJS setup and check in the getInitialProps() function of B if the store has the respective data using store.getState() and accordingly perform a fetch. This will also help you maintain the data in case of navigation from A to B and then performing a page refresh on B.
Setting up Redux can be a little tricky but this article basically sums it up.
add a comment |
You can setup Redux with your NextJS setup and check in the getInitialProps() function of B if the store has the respective data using store.getState() and accordingly perform a fetch. This will also help you maintain the data in case of navigation from A to B and then performing a page refresh on B.
Setting up Redux can be a little tricky but this article basically sums it up.
You can setup Redux with your NextJS setup and check in the getInitialProps() function of B if the store has the respective data using store.getState() and accordingly perform a fetch. This will also help you maintain the data in case of navigation from A to B and then performing a page refresh on B.
Setting up Redux can be a little tricky but this article basically sums it up.
answered Mar 28 at 5:41
Harshil ShahHarshil Shah
485 bronze badges
485 bronze badges
add a comment |
add a comment |
Adding to what Harshail Shah, you have the option of setting up Redux to allow for getInitialProps to read the data. But in addition to that, you have a lot of other options that you can try. Each of them have their advantages and disavantages, usually determined by how much data you are moving around.
1. Trying a pseudo-flash approach
In this approach, you temporarily store data in one getInitialProps using cookies and in the next page, you clear the cookie. One package you can use which automatically picks the cookie (whether client-side or server-side) is next-cookie So, your approach will be:
class Component1 extends React.Component
static async getInitialProps(ctx)
const isServer = !!ctx.req;
// Set the cookie here. You can use next-cookie or ctx.req.cookie
and then in the second component, you will have:
class Component2 extends React.Component
static async getInitialProps(ctx)
const cookies = NextCookie(ctx);
const desiredCookie = cookies['key_name'];
// You can then delete the cookie here so that the data won't be available the next time
2. Encrypting the data and passing it as a query parameter to the next page
In my opinion, this is responsible for a lot of long URLs we see around. Basically the idea is on click of a button, you convert the data either into a base64 string or use a very fast browser encrypter/decrypter to append the data to the new URL as a query parameter and read it in the next page.
The best approach will usually depend on how much data you are encrypting; and how long you want the data to persist.
add a comment |
Adding to what Harshail Shah, you have the option of setting up Redux to allow for getInitialProps to read the data. But in addition to that, you have a lot of other options that you can try. Each of them have their advantages and disavantages, usually determined by how much data you are moving around.
1. Trying a pseudo-flash approach
In this approach, you temporarily store data in one getInitialProps using cookies and in the next page, you clear the cookie. One package you can use which automatically picks the cookie (whether client-side or server-side) is next-cookie So, your approach will be:
class Component1 extends React.Component
static async getInitialProps(ctx)
const isServer = !!ctx.req;
// Set the cookie here. You can use next-cookie or ctx.req.cookie
and then in the second component, you will have:
class Component2 extends React.Component
static async getInitialProps(ctx)
const cookies = NextCookie(ctx);
const desiredCookie = cookies['key_name'];
// You can then delete the cookie here so that the data won't be available the next time
2. Encrypting the data and passing it as a query parameter to the next page
In my opinion, this is responsible for a lot of long URLs we see around. Basically the idea is on click of a button, you convert the data either into a base64 string or use a very fast browser encrypter/decrypter to append the data to the new URL as a query parameter and read it in the next page.
The best approach will usually depend on how much data you are encrypting; and how long you want the data to persist.
add a comment |
Adding to what Harshail Shah, you have the option of setting up Redux to allow for getInitialProps to read the data. But in addition to that, you have a lot of other options that you can try. Each of them have their advantages and disavantages, usually determined by how much data you are moving around.
1. Trying a pseudo-flash approach
In this approach, you temporarily store data in one getInitialProps using cookies and in the next page, you clear the cookie. One package you can use which automatically picks the cookie (whether client-side or server-side) is next-cookie So, your approach will be:
class Component1 extends React.Component
static async getInitialProps(ctx)
const isServer = !!ctx.req;
// Set the cookie here. You can use next-cookie or ctx.req.cookie
and then in the second component, you will have:
class Component2 extends React.Component
static async getInitialProps(ctx)
const cookies = NextCookie(ctx);
const desiredCookie = cookies['key_name'];
// You can then delete the cookie here so that the data won't be available the next time
2. Encrypting the data and passing it as a query parameter to the next page
In my opinion, this is responsible for a lot of long URLs we see around. Basically the idea is on click of a button, you convert the data either into a base64 string or use a very fast browser encrypter/decrypter to append the data to the new URL as a query parameter and read it in the next page.
The best approach will usually depend on how much data you are encrypting; and how long you want the data to persist.
Adding to what Harshail Shah, you have the option of setting up Redux to allow for getInitialProps to read the data. But in addition to that, you have a lot of other options that you can try. Each of them have their advantages and disavantages, usually determined by how much data you are moving around.
1. Trying a pseudo-flash approach
In this approach, you temporarily store data in one getInitialProps using cookies and in the next page, you clear the cookie. One package you can use which automatically picks the cookie (whether client-side or server-side) is next-cookie So, your approach will be:
class Component1 extends React.Component
static async getInitialProps(ctx)
const isServer = !!ctx.req;
// Set the cookie here. You can use next-cookie or ctx.req.cookie
and then in the second component, you will have:
class Component2 extends React.Component
static async getInitialProps(ctx)
const cookies = NextCookie(ctx);
const desiredCookie = cookies['key_name'];
// You can then delete the cookie here so that the data won't be available the next time
2. Encrypting the data and passing it as a query parameter to the next page
In my opinion, this is responsible for a lot of long URLs we see around. Basically the idea is on click of a button, you convert the data either into a base64 string or use a very fast browser encrypter/decrypter to append the data to the new URL as a query parameter and read it in the next page.
The best approach will usually depend on how much data you are encrypting; and how long you want the data to persist.
answered Mar 28 at 8:35
cr05s19xxcr05s19xx
6677 silver badges22 bronze badges
6677 silver badges22 bronze badges
add a comment |
add a comment |
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%2f55389893%2fpassing-data-between-pages-received-via-fetch-in-getinitialprops-of-a-page-compo%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
are you using react-router v4?
– Avinash Mahlawat
Mar 28 at 4:03
Sorry should have been clearer. Using Next.js
– Smarajit
Mar 28 at 4:04