Toggle Show / Hide is a common UI pattern in React Native used to conditionally display components such as text, views, forms, or images using state management.
useState hook for state management.&&) or Ternary operator (? :).Button or TouchableOpacity to trigger the toggle event.React Native re-renders the component whenever the state changes. By toggling a boolean variable between true and false, we can control the existence of UI elements.
// 1. Import useState
import React, { useState } from "react";
import { View, Text, Button } from "react-native";
const ToggleExample = () => {
// 2. Define State (initially false/hidden)
const [visible, setVisible] = useState(false);
return (
<View>
// 3. Button toggles state on press
<Button
title={visible ? "Hide Text" : "Show Text"}
onPress={() => setVisible(!visible)}
/>
// 4. Conditional Rendering
{visible && <Text>Hello React Native!</Text>}
</View>
);
};
export default ToggleExample;
When the button is pressed, the visible state flips. If true, the Text component is added to the screen. If false, it is removed entirely.
Use the simulator below. Notice how the Button Text changes and the Content appears based on the value of the State Variable.
!visible to easily flip the current boolean value.useState(false)).true).Button with a TouchableOpacity.