How to choose the appropriate screen to return to when there are multiple candidates in Next.js

To elaborate further, the question is as follows:
In Next.js, suppose there are multiple candidate screens as the source screen for a certain screen X. For the sake of explanation, let’s call these candidates screen A and screen B. Even after navigating through several screen transitions from screen X, how can I retrieve information about whether the initial source screen for screen X was screen A or screen B, and enable returning to the appropriate screen A or screen B from screen X? What kind of implementation should be considered?

The following is an illustration of the situation I would like to inquire about.

Illustration for explanation

In the above image, the light blue rectangles represent each screen, and for example, the notation “A: /a” indicates that the path for screen A is “/a”. This applies to other screens as well.

The rounded gray rectangles with text such as “Go to X” or “Go Back” represent buttons on the screen, and the black arrows originating from the buttons indicate the screen transitions that occur when the buttons are clicked. “Tr#1” and “Tr#2” refer to the first and second screen transitions, respectively.

As depicted in the diagram, it’s possible to navigate back and forth between screens X, Y, and Z multiple times.

I understand that implementations for screen transitions from Tr#1 to Tr#6 are feasible as follows. That is to say, by utilizing the router obtained through:

const router = useRouter()

each transition can be realized with the following code.

  • Tr#1: router.push(‘/x’)
  • Tr#2: router.push(‘/y’)
  • Tr#3: router.push(‘/z’)
  • Tr#4: router.push(‘/y’)
  • Tr#5: router.push(‘/x’)

It is understood that by creating button handlers containing each of the above and setting them to the onClick prop of each button, the transitions can be achieved.

However, I don’t understand how to implement the screen transition Tr#6. In the previous diagram, Tr#6 is indicated by a red dashed line, and there is a red diamond shape along that line. This diamond shape indicates a conditional branching. Therefore, it is necessary to determine whether the source screen when initially transitioning to screen X was screen A or screen B and perform the screen transition accordingly.

Based on the above example,my question is,

how can I implement code to allow selecting either screen A or screen B from the “Go Back” button on screen X and navigate to the appropriate one?
Thank you in advance.

What I’ve done is as follows:

  • I researched the Next official documentation to see if what I’m addressing in this question could be achieved.
  • I searched to see if anyone had tackled similar issues and come up with solutions.
  • Since I’ve often used Redux, I also considered creating a Redux slice to manage some sort of screen transition history, but if it’s possible to achieve using features provided by Next.js, I’d like to know that method.

New contributor

Yanling210 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