Practice and reinforce the concepts from Lesson 3
Practice state management by fixing a broken counter app with 5 deliberate bugs.
import React, { useState } from 'react';
import { View, Text, Button } from 'react-native';
const CounterApp = () => {
const [count, setCount] = useState(0);
const [step, setStep] = useState(1);
const increment = () => {
count = count + step; // Bug #1
};
const decrement = () => {
setCount(count - step);
};
const reset = () => {
setCount(); // Bug #2
};
return (
<View>
<Text>Count: {count}</Text>
<Text>Step: {steps}</Text> {/* Bug #3 */}
<Button title="+" onPress={increment} />
<Button title="-" onPress={decrement} />
<Button title="Reset" onPress={reset} />
<Button
title="Change Step"
onPress={() => setStep(step + 1)}
/>
{count > 10 && ( // Bug #4
<Text>You're doing great!</Text>
)}
<Text>Double: {count * 2}</Text> {/* Bug #5 - sometimes shows wrong */}
</View>
);
};
Your fixed app should:
Add a "Random" button that sets count to a random number between 1-100.
Complete this activity and submit your work through the Activity Submission Form