2021-04-15 10:55:50 -05:00
|
|
|
import React, { useState } from 'react';
|
|
|
|
|
|
|
|
/** a counter written in React */
|
|
|
|
export default function ReactCounter({ children }) {
|
2021-05-03 12:26:10 -06:00
|
|
|
const [count, setCount] = useState(0);
|
|
|
|
const add = () => setCount((i) => i + 1);
|
|
|
|
const subtract = () => setCount((i) => i - 1);
|
2021-04-15 10:55:50 -05:00
|
|
|
|
2021-05-03 12:26:10 -06:00
|
|
|
return (
|
|
|
|
<>
|
|
|
|
<div className="counter">
|
2021-04-15 10:55:50 -05:00
|
|
|
<button onClick={subtract}>-</button>
|
|
|
|
<pre>{count}</pre>
|
|
|
|
<button onClick={add}>+</button>
|
2021-05-03 12:26:10 -06:00
|
|
|
</div>
|
|
|
|
<div className="children">{children}</div>
|
|
|
|
</>
|
|
|
|
);
|
2021-04-15 10:55:50 -05:00
|
|
|
}
|