Can not submit a NEXT.js form that changes some variables in a context file that is exported to another display page

I have 3 main files in my next.js Project. My settings are: no /src, yes App Router, yes Typescript, contains app/ folder. My main goal is to display some values that are updated through a form, in another page using React Contexts.

File 1 has location components/AddressForm.tsx. The main purpose is to provide a form and when submitted, update formData values that is defined inn File 2 (FormContext.tsx):

"use client"

import React, { useState } from 'react';
import { useFormContext, CheckoutData } from '../context/FormContext';

const AddressForm: React.FC = () => {
  const { formData, setFormData } = useFormContext();
  const [localFormData, setLocalFormData] = useState<CheckoutData>(formData);

  // Update local form data on input change
  const handleInputChange = (event: React.ChangeEvent<HTMLInputElement>) => {
    const { id, value } = event.target;
    setLocalFormData(prevData => ({
      ...prevData,
      shippingAddress: {
        ...prevData.shippingAddress,
        [id]: value
      }
    }));
  };

  // Update context with local form data on form submission
  const handleFormSubmit = (event: React.FormEvent<HTMLFormElement>) => {
    event.preventDefault();
    setFormData(prevData => ({
      ...prevData,
      shippingAddress: {
        ...localFormData.shippingAddress
      }
    }));
    console.log('Form submitted with data:', localFormData);
  };

Below this we have the return() with the form.
File 2 is context/FormContext.tsx. This file contains export interfaces and a FormProvider that defines const[formData, setFormData) which is planned to be updated in File 1:

    "use client";

    import React, { createContext, useContext, useState } from 'react';

    export interface ShippingAddress {
      name: string;
      surname: string;
      email: string;
      gsmNumber: string;
      address: string;
      city: string;
      state: string;
      zip: string;
    }

    export interface BasketItems {
      id: string;
      name: string;
      category1: string;
      category2: string;
      itemType: string;
      price: number;
    }

    export interface CheckoutData {
      shippingAddress: ShippingAddress;
      billingAddress: ShippingAddress;
      basketItems: BasketItems;
      subtotal: number;
      shipping: number;
      discount: number;
      total: number;
    }

    interface FormContextProps {
      formData: CheckoutData;
      setFormData: React.Dispatch<React.SetStateAction<CheckoutData>>;
    }

    const FormContext = createContext<FormContextProps | undefined>(undefined);

    export const FormProvider: React.FC<{ children: React.ReactNode }> = ({ children }) => {
      const [formData, setFormData] = useState<CheckoutData>({
        shippingAddress: {
          name: '',
          surname: '',
          email: '',
          gsmNumber: '',
          address: '',
          city: '',
          state: '',
          zip: ''
        },
        billingAddress: {
          name: '',
          surname: '',
          email: '',
          gsmNumber: '',
          address: '',
          city: '',
          state: '',
          zip: ''
        },
        basketItems: {
          id: 'BI101',
          name: 'Product Title 1',
          category1: 'Collectibles',
          category2: 'Accessories',
          itemType: 'PHYSICAL',
          price: 49.99
        },
        subtotal: 49.99,
        shipping: 0,
        discount: 0,
        total: 49.99
      });

      return (
        <FormContext.Provider value={{ formData, setFormData }}>
          {children}
        </FormContext.Provider>
      );
    };

    export const useFormContext = () => {
      const context = useContext(FormContext);
      if (context === undefined) {
        throw new Error('useFormContext must be used within a FormProvider');
      }
      return context;
    };

File 3 only displays the data. I know for a fact that File 2 and File 3 is connected properly as the already defined subtotal and price values are perfectly diplayed in File 3. But the empty ones are never updated.

      const handleInputChange = (event: React.ChangeEvent<HTMLInputElement>) => {
        const { id, value } = event.target;
        setLocalFormData(prevData => ({
          ...prevData,
          shippingAddress: {
            ...prevData.shippingAddress,
            [id]: value
          }
        }));
      };

      // Update context with local form data on form submission
      const handleFormSubmit = (event: React.FormEvent<HTMLFormElement>) => {
        event.preventDefault();
        setFormData(prevData => ({
          ...prevData,
          shippingAddress: {
            ...localFormData.shippingAddress
          }
        }));
        console.log('Form submitted with data:', localFormData);
      };

I am guessing the problem arises from the above section as there is no log:
Form submitted with data:', localFormData when I submit the form through a type = submit button inside a tag in File 1. I am sure that the problem arises from a fault that disables the code to submit the form. Explaining the non-existing log, and the no display

New contributor

mic_faraday is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật