0
0
React Nativemobile~20 mins

Switch and checkbox patterns in React Native - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Switch and Checkbox Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
ui_behavior
intermediate
1: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>
  );
}
Afalse
Btrue
Cundefined
Dnull
Attempts:
2 left
💡 Hint
The toggleSwitch function flips the current boolean state.
📝 Syntax
intermediate
1: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>
  );
}
A(newValue) => setChecked(newValue)
B() => setChecked(undefined)
C() => setChecked(checked)
D() => setChecked(!checked)
Attempts:
2 left
💡 Hint
The onValueChange handler receives the new boolean value.
lifecycle
advanced
2: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>
  );
}
ADisabled
BEnabled
Cundefined
DError
Attempts:
2 left
💡 Hint
Each toggle flips the boolean state.
navigation
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?
AThe checkbox state causes a navigation error on return.
BThe checkbox state persists automatically without extra code.
CThe checkbox state is shared across all screens automatically.
DThe checkbox state resets to its initial value each time the screen is visited.
Attempts:
2 left
💡 Hint
Local state in a component resets when the component unmounts and remounts.
🔧 Debug
expert
2: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>
  );
}
AThe useState hook is missing an initial value.
BThe Switch component requires a different prop name than value.
CThe onValueChange handler sets the state to the current value, so no change occurs.
DThe Switch component must be wrapped in a TouchableOpacity.
Attempts:
2 left
💡 Hint
Check what value is passed to setEnabled inside onValueChange.