File tree 2 files changed +21
-8
lines changed
epic-react-kent-c-dodds/04-Advanced-React-Patterns/src
2 files changed +21
-8
lines changed Original file line number Diff line number Diff line change @@ -8,26 +8,41 @@ function useToggle() {
8
8
const [ on , setOn ] = React . useState ( false )
9
9
const toggle = ( ) => setOn ( ! on )
10
10
11
- const togglerProps = { 'aria-pressed' : on , onClick : toggle }
11
+ function getTogglerProps ( { onClick, ...props } = { } ) {
12
+ return {
13
+ 'aria-pressed' : on ,
14
+ onClick : ( ) => {
15
+ onClick && onClick ( )
16
+ toggle ( )
17
+ } ,
18
+ ...props ,
19
+ }
20
+ }
21
+
12
22
// 🐨 Add a property called `togglerProps`. It should be an object that has
13
23
// `aria-pressed` and `onClick` properties.
14
24
// 💰 {'aria-pressed': on, onClick: toggle}
15
- return { on, toggle, togglerProps }
25
+ return { on, toggle, getTogglerProps }
16
26
}
17
27
18
28
function App ( ) {
19
- const { on, togglerProps } = useToggle ( )
29
+ const { on, getTogglerProps } = useToggle ( )
20
30
return (
21
31
< div >
22
- < Switch on = { on } { ...togglerProps } />
32
+ < Switch { ...getTogglerProps ( { on } ) } />
23
33
< hr />
24
- < button aria-label = "custom-button" { ...togglerProps } >
34
+ < button
35
+ { ...getTogglerProps ( {
36
+ 'aria-label' : 'custom-button' ,
37
+ onClick : ( ) => console . info ( 'onButtonClick' ) ,
38
+ id : 'custom-button-id' ,
39
+ } ) }
40
+ >
25
41
{ on ? 'on' : 'off' }
26
42
</ button >
27
43
</ div >
28
44
)
29
45
}
30
-
31
46
export default App
32
47
33
48
/*
Original file line number Diff line number Diff line change @@ -23,8 +23,6 @@ class Switch extends React.Component {
23
23
...props
24
24
} = this . props
25
25
26
- console . log ( this . props ) ;
27
-
28
26
const btnClassName = [
29
27
className ,
30
28
'toggle-btn' ,
You can’t perform that action at this time.
0 commit comments