Skip to content

Commit

Permalink
Complete exercise 4
Browse files Browse the repository at this point in the history
  • Loading branch information
nicmalegre committed Oct 13, 2022
1 parent d535ff8 commit 24967b0
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 4 deletions.
46 changes: 46 additions & 0 deletions src/exercise/04.extra-1.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
// Prop Collections and Getters
// http://localhost:3000/isolated/exercise/04.js

import * as React from 'react'
import {Switch} from '../switch'

function useToggle() {
const [on, setOn] = React.useState(false)
const toggle = () => setOn(!on)

const getTogglerProps = ({onClick, ...props} = {}) => ({
'aria-pressed': on,
onClick: () => {
onClick?.()
toggle()
},
...props,
})
return {on, toggle, getTogglerProps}
}

function App() {
const {on, getTogglerProps} = useToggle()
return (
<div>
<Switch {...getTogglerProps({on})} />
<hr />
<button
{...getTogglerProps({
'aria-label': 'custom-button',
onClick: () => console.info('onButtonClick'),
id: 'custom-button-id',
})}
>
{on ? 'on' : 'off'}
</button>
</div>
)
}

export default App

/*
eslint
no-unused-vars: "off",
*/
6 changes: 2 additions & 4 deletions src/exercise/04.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,8 @@ function useToggle() {
const [on, setOn] = React.useState(false)
const toggle = () => setOn(!on)

// 🐨 Add a property called `togglerProps`. It should be an object that has
// `aria-pressed` and `onClick` properties.
// 💰 {'aria-pressed': on, onClick: toggle}
return {on, toggle}
const togglerProps = {'aria-pressed': on, onClick: toggle}
return {on, toggle, togglerProps}
}

function App() {
Expand Down

0 comments on commit 24967b0

Please sign in to comment.