0.0.20 • Published 5 years ago

@swyx/hooks v0.0.20

Weekly downloads
47
License
MIT
Repository
github
Last release
5 years ago

hooks

swyx's personal hooks library

NPM JavaScript Style Guide

Install

npm install --save @swyx/hooks

Usage

// useProduceState demo
const [state, setState] = useProduceState({
  todos: [],
  showMenu: false
})
const closeModal = e => {
  setState(draft => (draft.showMenu = false))
}

// useLocalStorage
const [item, setItem, removeItem] = useLocalStorage("yourspecialkeyhere", callbackOnItemChange)

// useInput demo
export default function InputArea({ onSubmit, defaultValue }) {
  const { setValue, ...inputProps } = useInput(defaultValue)
  const handleNewTodoKeyDown = event => {
    if (event.keyCode !== 13) return
    event.preventDefault()
    var val = event.target.value.trim()
    if (val) {
      onSubmit(val)
      setValue("")
    }
  }
  return <input onKeyDown={handleNewTodoKeyDown} {...inputProps} autoFocus={true} />
}

// note:
// There is also a `useCheckInput` hook that just has special return values for the platform
// we will warn you if you dont do this
// there are also extra otions you can pass to useInput as a second arg.

// useLoading demo
function App() {
  const [isLoading, load] = useLoading()
  const [state, setState] = React.useState("you shouldnt see this")
  React.useEffect(() => {
    load(pingAPI(2000)).then(() => setState("hello there"))
  })
  return isLoading ? <div>Loading</div> : <div>{state}</div>
}

function pingAPI(time) {
  return new Promise(resolve => setTimeout(resolve, time))
}

// useKeyDown demo
export default function Menu(props) {
  useKeydown("Escape", () => props.showMenu && props.handleModalClose())
  // ...
}

Video Docs:

Acknowledgements

bootstrapped with Travis Fischer's wonderful https://www.npmjs.com/package/create-react-library

License

MIT © sw-yx

0.0.20

5 years ago

0.0.19

5 years ago

0.0.18

5 years ago

0.0.17

5 years ago

0.0.16

5 years ago

0.0.14

5 years ago

0.0.13

5 years ago

0.0.12

5 years ago

0.0.10

5 years ago

0.0.9

5 years ago

0.0.8

5 years ago

0.0.7

5 years ago

0.0.6

5 years ago

0.0.5

5 years ago

0.0.4

5 years ago

0.0.3

5 years ago

0.0.2

5 years ago

0.0.1

5 years ago