How can I handle hardware back button navigation in a React Native Wizard component without modifying the child component?

I’m working on a React Native project where I have a custom Wizard component that allows users to navigate through different steps in a form. The Wizard component is used within a parent component like this:

<Wizard
  activePage={activePage}
  steps={[
    {number: 1, title: 'Resume Header'},
    {number: 2, title: 'Experience'},
    {number: 3, title: 'Education'},
    {number: 4, title: 'Skills and Details'},
  ]}
  onStepPress={handleStepPress}
/>

he Wizard component handles the rendering of steps and allows navigation between them by clicking on step numbers. Now, I want to implement functionality where pressing the Android hardware back button will navigate to the previous step in the wizard (e.g., if the user is on step 2, pressing the back button should take them to step 1).

Here’s what I’ve tried:
To handle this, I added a backHandler function in the parent component and passed it down as a prop to the Wizard component. The parent component looks like this:

import React, { useState, useEffect } from 'react';
import { BackHandler } from 'react-native';
import Wizard from './Wizard';

const ParentComponent = () => {
  const [activePage, setActivePage] = useState(1);

  const handleStepPress = (pageNumber) => {
    setActivePage(pageNumber);
    return true;
  };

  const backAction = () => {
    if (activePage > 1) {
      handleStepPress(activePage - 1);
      return true;
    }
    return false;
  };

  useEffect(() => {
    const backHandler = BackHandler.addEventListener(
      'hardwareBackPress',
      backAction,
    );

    return () => backHandler.remove();
  }, [activePage]);

  return (
    <Wizard
      activePage={activePage}
      steps={[
        { number: 1, title: 'Resume Header' },
        { number: 2, title: 'Experience' },
        { number: 3, title: 'Education' },
        { number: 4, title: 'Skills and Details' },
      ]}
      onStepPress={handleStepPress}
      backHandler={backAction} // Passed down to Wizard
    />
  );
};

export default ParentComponent;

In the Wizard component, I added a listener for the hardware back button using the passed backHandler function:


import React, { useEffect, useState } from 'react';
import { View, Text, TouchableOpacity, StyleSheet, BackHandler } from 'react-native';
import { Colors } from '../styles/Colors';

interface WizardProps {
  activePage: number;
  steps: { number: number; title: string }[];
  onStepPress: (pageNumber: number) => boolean;
  backHandler: () => boolean;
}

const Wizard: React.FC<WizardProps> = ({ activePage, steps, onStepPress, backHandler }) => {
  const [currentStep, setCurrentStep] = useState(activePage);

  useEffect(() => {
    setCurrentStep(activePage);
  }, [activePage]);

  useEffect(() => {
    const backHandlerListener = BackHandler.addEventListener(
      'hardwareBackPress',
      backHandler,
    );

    return () => backHandlerListener.remove();
  }, [currentStep, backHandler]);

  return (
    <View>
      {/* Step rendering code */}
    </View>
  );
};

export default Wizard;

Problem:
This approach solves my problem, but I feel like it’s not the best practice. I had to modify the Wizard component to handle the back button, which seems unnecessary since the parent component should ideally manage this. I want to avoid disturbing the child component if it’s not necessary.

Expected Solution:
Is there a better way to handle the hardware back button in the parent component without having to modify the Wizard component? I’m looking for a solution that keeps the Wizard component generic and reusable without being tightly coupled to the back button logic.

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