Skip to content

Instantly share code, notes, and snippets.

@Uzwername
Created August 27, 2020 21:19
Show Gist options
  • Save Uzwername/9a477b58b4be4162453d9ceda179cf82 to your computer and use it in GitHub Desktop.
Save Uzwername/9a477b58b4be4162453d9ceda179cf82 to your computer and use it in GitHub Desktop.
Functions vs. Components - App.js with description, CounterWithWeekday & a bug
const App = () => {
const [total, setTotal] = useState(0);
const incrementTotal = () => setTotal((currentTotal) => currentTotal + 1);
const Description = () => (
<p>I like coding counters! Sum of all counters is now {total}</p>
);
const CounterWithWeekday = (props) => {
let today;
switch (new Date().getDay()) {
case 0:
case 6:
today = "a weekend!";
break;
case 1:
today = "Monday";
break;
case 2:
today = "Tuesday";
break;
default:
today = "some day close to a weekend!";
break;
}
return (
<div>
<Counter {...props} />
<br />
<span>Today is {today}</span>
</div>
);
};
return (
<div className="App">
<div>
<h4>Total Clicks: {total}</h4>
<Description />
</div>
<div className="CountersContainer">
<Counter onClick={incrementTotal} />
<CounterWithWeekday onClick={incrementTotal} />
<Counter onClick={incrementTotal} />
</div>
</div>
);
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment