1
- import React from 'react'
1
+ import React , { useState } from 'react'
2
2
import { storiesOf } from '@storybook/react'
3
3
import { action } from '@storybook/addon-actions'
4
4
import { withKnobs , text } from '@storybook/addon-knobs'
@@ -8,16 +8,33 @@ import ButtonReadme from '../README.md'
8
8
9
9
import Button from '.'
10
10
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
+ )
15
31
}
16
32
17
33
storiesOf ( 'Button' , module )
18
34
. addDecorator ( withKnobs )
19
35
. addDecorator ( withReadme ( ButtonReadme ) )
20
36
. addWithJSX ( 'default' , ( ) => {
21
37
const buttonText = text ( 'text' , 'Click me' )
22
- return < Button onClick = { onClick } > { buttonText } </ Button >
38
+ return < Button onClick = { onClickHandler } > { buttonText } </ Button >
23
39
} )
40
+ . addWithJSX ( 'with click counter (hooks)' , ( ) => < StoryComponent /> )
0 commit comments