Class Component Counter
ReactJS
Medium
6 views
Problem Description
Create a small counter using a class component (for practice).
Output Format
Render a React component.
Constraints
Use setState correctly.
Official Solution
import React from 'react';
class Counter extends React.Component {
state = { count: 0 };
render() {
return (
<div style={{ padding: 16 }}>
<strong>meetcode</strong>
<div style={{ marginTop: 8 }}>Count: {this.state.count}</div>
<button
type='button'
onClick={() => this.setState((s) => ({ count: s.count + 1 }))}
style={{ marginTop: 10, padding: '10px 14px', borderRadius: 12, border: 0, background: '#0b5', color: '#fff' }}
>
Add
</button>
</div>
);
}
}
export default function App() {
return <Counter />;
}
Solutions (0)
No solutions submitted yet. Be the first!
No comments yet. Start the discussion!