Skip to content

Instantly share code, notes, and snippets.

@chriskavanagh
Last active February 20, 2024 02:09
Show Gist options
  • Save chriskavanagh/0cfe1d9ffaa06cdf2dedbc9dd64f4876 to your computer and use it in GitHub Desktop.
Save chriskavanagh/0cfe1d9ffaa06cdf2dedbc9dd64f4876 to your computer and use it in GitHub Desktop.
composition instead of context
import { useState } from "react";
const Container = ({ children }) => <div>{children}</div>;
const AddOneButton = ({ setCounter }) => (
<div>
<button onClick={() => setCounter((v) => v + 1)}>Add One</button>
</div>
);
const Counter = ({ counter }) => {
return <div>Counter: {counter}</div>;
};
export default function CounterUseState() {
const [counter, setCounter] = useState(0);
return (
<div>
<Container>
<AddOneButton setCounter={setCounter} />
<Counter counter={counter} />
</Container>
</div>
);
}
import "./styles.css";
import { useState } from "react";
import CounterUseState from "./counter-use-state";
export default function App() {
return (
<div className="App">
<CounterUseState />
</div>
);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment