Challenge - 5 Problems
Switch and Checkbox Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ ui_behavior
intermediate1:30remaining
Toggle Switch State Update
What will be the value of
isEnabled after toggling the switch once in this React Native component?React Native
import React, { useState } from 'react'; import { Switch, View, Text } from 'react-native'; export default function App() { const [isEnabled, setIsEnabled] = useState(false); const toggleSwitch = () => setIsEnabled(previousState => !previousState); return ( <View> <Switch onValueChange={toggleSwitch} value={isEnabled} /> <Text>{isEnabled ? 'On' : 'Off'}</Text> </View> ); }
Attempts:
2 left
💡 Hint
The toggleSwitch function flips the current boolean state.
✗ Incorrect
The initial state is false. Toggling once flips it to true, so the switch value becomes true.
📝 Syntax
intermediate1:30remaining
Correct Checkbox State Handling
Which option correctly updates the checkbox state when the user taps it in React Native?
React Native
import React, { useState } from 'react'; import { CheckBox, View } from 'react-native'; export default function App() { const [checked, setChecked] = useState(false); return ( <View> <CheckBox value={checked} onValueChange={/* Which option here? */} /> </View> ); }
Attempts:
2 left
💡 Hint
The onValueChange handler receives the new boolean value.
✗ Incorrect
The onValueChange callback provides the new checked value. Using it directly updates state correctly.
❓ lifecycle
advanced2:00remaining
Switch State Persistence on Re-render
Given this component, what will be the displayed text after pressing the switch twice?
React Native
import React, { useState } from 'react'; import { Switch, View, Text } from 'react-native'; export default function App() { const [isOn, setIsOn] = useState(false); const toggle = () => { setIsOn(!isOn); }; return ( <View> <Switch value={isOn} onValueChange={toggle} /> <Text>{isOn ? 'Enabled' : 'Disabled'}</Text> </View> ); }
Attempts:
2 left
💡 Hint
Each toggle flips the boolean state.
✗ Incorrect
Starting false, first toggle sets true, second toggle sets false again, so text shows 'Disabled'.
advanced
2:00remaining
Checkbox State and Navigation Behavior
In a React Native app using React Navigation, if a checkbox state is stored locally in a screen component, what happens to the checkbox state when navigating away and back to that screen?
Attempts:
2 left
💡 Hint
Local state in a component resets when the component unmounts and remounts.
✗ Incorrect
When navigating away, the screen unmounts by default, so local state resets on return unless stored globally or persisted.
🔧 Debug
expert2:30remaining
Why does this Switch not toggle visually?
This React Native Switch component does not visually toggle when pressed. What is the cause?
React Native
import React, { useState } from 'react'; import { Switch, View } from 'react-native'; export default function App() { const [enabled, setEnabled] = useState(false); return ( <View> <Switch value={enabled} onValueChange={() => setEnabled(enabled)} /> </View> ); }
Attempts:
2 left
💡 Hint
Check what value is passed to setEnabled inside onValueChange.
✗ Incorrect
The handler sets state to the same value, so the state never changes and the switch never visually toggles.