Build: Counter with useReducer
A simple counter app that uses the useReducer hook to increase, decrease, and reset the count.
Target UI
--------------------- | Counter App | --------------------- | | | 0 | | | | [+] [-] Reset | ---------------------
--------------------- | Counter App | --------------------- | | | 0 | | | | [+] [-] Reset | ---------------------
import React, { useReducer } from 'react'; import { View, Text, Button, StyleSheet } from 'react-native'; const initialState = 0; function reducer(state, action) { // TODO: Implement reducer logic here return state; } export default function Counter() { const [count, dispatch] = useReducer(reducer, initialState); return ( <View style={styles.container}> <Text style={styles.title}>Counter App</Text> <Text style={styles.count}>{count}</Text> <View style={styles.buttons}> {/* TODO: Add buttons to dispatch actions */} </View> </View> ); } const styles = StyleSheet.create({ container: { flex: 1, justifyContent: 'center', alignItems: 'center' }, title: { fontSize: 24, marginBottom: 20 }, count: { fontSize: 48, marginBottom: 20 }, buttons: { flexDirection: 'row', justifyContent: 'space-between', width: 200 } });
import React, { useReducer } from 'react'; import { View, Text, Button, StyleSheet } from 'react-native'; const initialState = 0; function reducer(state, action) { switch (action.type) { case 'increment': return state + 1; case 'decrement': return state - 1; case 'reset': return 0; default: return state; } } export default function Counter() { const [count, dispatch] = useReducer(reducer, initialState); return ( <View style={styles.container}> <Text style={styles.title}>Counter App</Text> <Text style={styles.count}>{count}</Text> <View style={styles.buttons}> <Button title="+" onPress={() => dispatch({ type: 'increment' })} /> <Button title="-" onPress={() => dispatch({ type: 'decrement' })} /> <Button title="Reset" onPress={() => dispatch({ type: 'reset' })} /> </View> </View> ); } const styles = StyleSheet.create({ container: { flex: 1, justifyContent: 'center', alignItems: 'center' }, title: { fontSize: 24, marginBottom: 20 }, count: { fontSize: 48, marginBottom: 20 }, buttons: { flexDirection: 'row', justifyContent: 'space-between', width: 200 } });
We use the useReducer hook to manage the counter state instead of useState. The reducer function takes the current state and an action, then returns the new state based on the action type.
We handle three actions: increment adds 1, decrement subtracts 1, and reset sets the count back to 0.
The buttons dispatch these actions when pressed, updating the count displayed on the screen.
--------------------- | Counter App | --------------------- | | | 0 | | | | [+] [-] Reset | ---------------------
useReducer hook in React Native?useReducer is designed to handle complex state changes by using a reducer function and dispatching actions.useReducer.useReducer in a React Native component?useReducer returns an array with two elements: current state and dispatch function.const [state, dispatch] is correct.state.count after dispatching { type: 'increment' }?
const initialState = { count: 0 };
function reducer(state, action) {
switch (action.type) {
case 'increment':
return { count: state.count + 1 };
case 'decrement':
return { count: state.count - 1 };
default:
return state;
}
}
const [state, dispatch] = useReducer(reducer, initialState);
dispatch({ type: 'increment' });useReducer setup:
const initialState = { count: 0 };
function reducer(state, action) {
if (action.type === 'increment') {
state.count += 1;
return state;
}
return state;
}
const [state, dispatch] = useReducer(reducer, initialState);
dispatch({ type: 'increment' });state.count directly, which is not recommended in React.isVisible state using useReducer. Which reducer function correctly toggles the state when dispatching { type: 'toggle' }?isVisible toggled using !state.isVisible.Pass the test and this pattern moves unknown