Skip to content

Commit bb75c64

Browse files
committed
prop collections and getters
1 parent 96f6e3e commit bb75c64

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

epic-react-kent-c-dodds/04-Advanced-React-Patterns/src/exercise/04.js

+21-6
Original file line numberDiff line numberDiff line change
@@ -8,26 +8,41 @@ function useToggle() {
88
const [on, setOn] = React.useState(false)
99
const toggle = () => setOn(!on)
1010

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+
1222
// 🐨 Add a property called `togglerProps`. It should be an object that has
1323
// `aria-pressed` and `onClick` properties.
1424
// 💰 {'aria-pressed': on, onClick: toggle}
15-
return {on, toggle, togglerProps}
25+
return {on, toggle, getTogglerProps}
1626
}
1727

1828
function App() {
19-
const {on, togglerProps} = useToggle()
29+
const {on, getTogglerProps} = useToggle()
2030
return (
2131
<div>
22-
<Switch on={on} {...togglerProps} />
32+
<Switch {...getTogglerProps({on})} />
2333
<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+
>
2541
{on ? 'on' : 'off'}
2642
</button>
2743
</div>
2844
)
2945
}
30-
3146
export default App
3247

3348
/*

epic-react-kent-c-dodds/04-Advanced-React-Patterns/src/switch.js

-2
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,6 @@ class Switch extends React.Component {
2323
...props
2424
} = this.props
2525

26-
console.log(this.props);
27-
2826
const btnClassName = [
2927
className,
3028
'toggle-btn',

0 commit comments

Comments
 (0)