Search code examples
react-nativereact-native-component

How to add a screen switcher component in react native


Being a newbie to the field of app development, I actually don't know the name of this UI component 1.

This component basically act as a switcher between two screen, which shows up Explore screen when Explore is clicked and My Community screen when My Community is clicked

Someone please help me to know what to call this component and which npm package need to be used to deploy it.

THANKS IN ADVANCE!!!


Solution

  • You can use the switch component and its boolean value to conditionally display different screens/components.

    Basic example using setState:

    const [isEnabled, setIsEnabled] = useState(false);
    const toggleSwitch = () => setIsEnabled(previousState => !previousState);
      
    return (
      <View>
        <Switch
            onValueChange={toggleSwitch}
            value={isEnabled}
          />
          {isEnabled ? <Text>Placerholder Screen 1</Text> : <Text>Placerholder Screen 2</Text>}
        </View>
      );
    

    But in this situation I would advise you to use Tabs instead of a Switch. React Navigation is a great library to help you with this. You may want to look at the bottom tabs, material bottom tabs or material top tabs.