I have started using NextJS and React recently.
I am trying to implement the multi page forms in my app, but I don't know how can I persist and pass the form data from one page to another page.
There are many guidelines about using state management libraries and context apis but they all are either in reactjs or defines the use between different components.
My _app.js looks like this:
const AppComponent = ({ Component, pageProps, currentUser }) => {
return (
<div>
<AppWrapper>
<Header currentUser={currentUser} />
<Component {...pageProps} />
</AppWrapper>
</div>
);
};
AppComponent.getInitialProps = async (appContext) => {
const client = buildClient(appContext.ctx);
const { data } = await client.get('/api/client/currentuser');
let pageProps = {};
if (appContext.Component.getInitialProps) {
pageProps = await appContext.Component.getInitialProps(appContext.ctx);
}
return {
pageProps,
...data,
};
};
export default AppComponent
context state.js
import { createContext, useContext } from 'react';
const AppContext = createContext();
export function AppWrapper({ children }) {
let sharedState = {
/* whatever you want */
};
return (
<AppContext.Provider value={sharedState}>{children}</AppContext.Provider>
);
}
export function useAppContext() {
return useContext(AppContext);
}
Now I need to save the data the of the form on page 1 (name, surname) and pass it to the page 2 where (email and phone) and finally a summary where I want to have all the fields. How can I set the shared_state from the following page?
step1.js import React from "react";
function Step1(props) {
return (
<div>
<p>Name: <input name="name" /></p>
<p>Surname: <input name="surname" /></p>
</div>
);
}
export default Step1;
step2.js
import React from "react";
export default function Step2(props) {
return (
<div>
<p>Email: <input name="email" /></p>
<p>Phone: <input name="Phone" /></p>
</div>
);
How can I set a state from user data in step1 and pass the data to step2. Can anyone help me out with this?