diff options
Diffstat (limited to 'example/index.tsx')
-rw-r--r-- | example/index.tsx | 38 |
1 files changed, 38 insertions, 0 deletions
diff --git a/example/index.tsx b/example/index.tsx new file mode 100644 index 0000000..c967d9d --- /dev/null +++ b/example/index.tsx @@ -0,0 +1,38 @@ +import React, { useState } from "react" +import { Provider, createContainer } from "../src/unstated" +import { render } from "react-dom" + +function useCounter() { + let [count, setCount] = useState(0) + let decrement = () => setCount(count - 1) + let increment = () => setCount(count + 1) + return { count, decrement, increment } +} + +let Counter = createContainer(useCounter) + +function CounterDisplay() { + let counter = Counter.useContainer() + return ( + <> + <button onClick={counter.decrement}>-</button> + <span>{counter.count}</span> + <button onClick={counter.increment}>+</button> + </> + ) +} + +function App() { + return ( + <Provider> + <CounterDisplay /> + <div> + <div> + <CounterDisplay /> + </div> + </div> + </Provider> + ) +} + +render(<App />, document.getElementById("root")) |