index.js
import React from "react";
import ReactDOM from "react-dom/client";
import AppCounter from "./AppCounter";
import "./index.css";
const root = ReactDOM.createRoot(document.getElementById("root"));
root.render(
<React.StrictMode>
<AppCounter />
</React.StrictMode>
);
AppCounter.jsx
import { useState } from "react";
import Counter from "./components/Counter";
export default function AppCounter() {
const [count, setCount] = useState(0);
const addTotalCount = () => {
setCount((prev) => {
return prev + 1;
});
};
return (
<div>
<div className="container">
<div className="banner">
Total Count: {count} {count > 10 ? "good" : "bad"}
</div>
</div>
<Counter addTotalCount={addTotalCount} />
<Counter addTotalCount={addTotalCount} />
</div>
);
}
Counter.jsx
import { useState } from "react";
export default function Counter({ addTotalCount }) {
const [num, setNum] = useState(0);
return (
<div className="container">
<div className="counter">
<span className="number">{num}</span>
<button
className="button"
onClick={() => {
setNum((prev) => {
return prev + 1;
});
addTotalCount();
}}
>
Add +
</button>
</div>
</div>
);
}
View