Skip to content
Advertisement

How to pass form fields from one page to another page in ReactJS?

Checkout.js

This is the checkout.js file. In this file, I returned the Checkout Form, and Now I want to use the form fields of the checkout form page.

The question is how will I get the form fields of the CheckoutForm page and use them on this page which is checkout.js. Because I want to submit the form data in the database. But the form is on another page and the submit button is on another page e.g. (checkout.js page)

JavaScript

CheckoutForm.js

In this file I used the Form I want to access this form fields ( for example first name, last name, and other fields ) in the checkout.js file. How can I do that? Any suggestions?

JavaScript

Advertisement

Answer

Here is my suggestion, you pass addressValues object and changeAddressValue function to CheckoutForm.js from checkout.js, addressValues is an object that has all values that user fills in the AddressForm i.e firstname, lastname, address etc, this object is controlled by a state in checkout.js, whenever user changes a value in the AddressForm/CheckoutForm, changeAddressValue function updates a values – addressFormValues in the checkout.js, this will make it easy when you submit the form on checkout.js because all the values will have already been populated on the addressFormValues state i.e const [addressFormValues, setAddressFormValues] = React.useState({});

Here is my solution:

CheckoutForm.js

JavaScript

PaymentForm.js

JavaScript

Checkout.js

JavaScript
Advertisement