This repository has been archived by the owner on Dec 29, 2022. It is now read-only.
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
6 changed files
with
99 additions
and
12 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
import React, { useState } from 'react'; | ||
import { createRexStore } from 'rex-state'; | ||
|
||
const useCounterHook = (initialCount?: number) => { | ||
const [count, setCount] = useState(initialCount || 0); | ||
|
||
const increaseCount = () => setCount(count + 1); | ||
const decreaseCount = () => setCount(count - 1); | ||
|
||
return { | ||
count, | ||
increaseCount, | ||
decreaseCount, | ||
}; | ||
}; | ||
|
||
const { useStore: useCounter, RexProvider: CountProvider } = createRexStore( | ||
useCounterHook | ||
); | ||
|
||
const CountDisplay = () => { | ||
const { count } = useCounter(); | ||
|
||
return <h1>{count}</h1>; | ||
}; | ||
|
||
const Controls = () => { | ||
const { increaseCount, decreaseCount } = useCounter(); | ||
|
||
return ( | ||
<> | ||
<button onClick={decreaseCount}> | ||
<span role="img" aria-label="subtract"> | ||
➖ | ||
</span> | ||
</button> | ||
<button onClick={increaseCount}> | ||
<span role="img" aria-label="add"> | ||
➕ | ||
</span> | ||
</button> | ||
</> | ||
); | ||
}; | ||
|
||
export const CounterWithInitialValue = () => { | ||
return ( | ||
<CountProvider value={10}> | ||
<CountDisplay /> | ||
<Controls /> | ||
</CountProvider> | ||
); | ||
}; | ||
|
||
export const CounterWithoutInitialValue = () => { | ||
return ( | ||
<CountProvider> | ||
<CountDisplay /> | ||
<Controls /> | ||
</CountProvider> | ||
); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
import React from 'react'; | ||
import type { Story, Meta } from '@storybook/react/types-6-0'; | ||
import { CounterWithInitialValue } from './Counter'; | ||
|
||
export default { | ||
title: 'Example/Counter with Initial Value', | ||
component: CounterWithInitialValue, | ||
} as Meta; | ||
|
||
const Template: Story<{}> = (args) => <CounterWithInitialValue {...args} />; | ||
|
||
export const WithInitialValue = Template.bind({}); | ||
WithInitialValue.args = {}; |
13 changes: 13 additions & 0 deletions
13
example/src/stories/CounterWithoutInitialValue.stories.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
import React from 'react'; | ||
import type { Story, Meta } from '@storybook/react/types-6-0'; | ||
import { CounterWithoutInitialValue } from './Counter'; | ||
|
||
export default { | ||
title: 'Example/Counter without Initial Value', | ||
component: CounterWithoutInitialValue, | ||
} as Meta; | ||
|
||
const Template: Story<{}> = (args) => <CounterWithoutInitialValue {...args} />; | ||
|
||
export const WithoutInitialValue = Template.bind({}); | ||
WithoutInitialValue.args = {}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters