Skip to content

Commit 08082b9

Browse files
committed
implement simple useState (react hook) example
1 parent 4a902ac commit 08082b9

File tree

1 file changed

+23
-6
lines changed

1 file changed

+23
-6
lines changed

packages/button/src/index.stories.js

+23-6
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import React from 'react'
1+
import React, { useState } from 'react'
22
import { storiesOf } from '@storybook/react'
33
import { action } from '@storybook/addon-actions'
44
import { withKnobs, text } from '@storybook/addon-knobs'
@@ -8,16 +8,33 @@ import ButtonReadme from '../README.md'
88

99
import Button from '.'
1010

11-
// the only reason onClick was written like this, and not just
12-
// `onClick = action('clicked')` is due to a cleaner export in the JSX tab
13-
const onClick = e => {
14-
action('clicked')(e)
11+
const onClickHandler = e => action('clicked')(e)
12+
13+
const StoryComponent = () => {
14+
// Declare a new state variable, which we'll call "count" with inital state 0
15+
const [count, setCount] = useState(0)
16+
17+
const buttonText = text('text', 'Click me')
18+
// the only reason onClick was written like this, and not just
19+
// `onClick = action('clicked')` is due to a cleaner export in the JSX tab
20+
const onClick = e => {
21+
setCount(count + 1)
22+
onClickHandler(e)
23+
}
24+
25+
return (
26+
<div>
27+
<p>You clicked {count} times</p>
28+
<Button onClick={onClick}>{buttonText}</Button>
29+
</div>
30+
)
1531
}
1632

1733
storiesOf('Button', module)
1834
.addDecorator(withKnobs)
1935
.addDecorator(withReadme(ButtonReadme))
2036
.addWithJSX('default', () => {
2137
const buttonText = text('text', 'Click me')
22-
return <Button onClick={onClick}>{buttonText}</Button>
38+
return <Button onClick={onClickHandler}>{buttonText}</Button>
2339
})
40+
.addWithJSX('with click counter (hooks)', () => <StoryComponent />)

0 commit comments

Comments
 (0)