2.1.45 • Published 8 months ago

@slimr/hooks v2.1.45

Weekly downloads
-
License
ISC
Repository
github
Last release
8 months ago

🪶 @slimr/hooks npm package

DEPRECATED: Merged into @slimr/react

A collection of tiny, useful react hooks

Context

@slimr is a set of slim React (hence '@slimr') libs:

  • @slimr/css - Framework agnostic css-in-js features inspired by the popular Emotion lib
  • @slimr/forms - A minimalistic form hook
  • @slimr/hooks - A collection of useful 1st and third party react hooks
  • @slimr/markdown - A simple component and slim markdown-to-html parser
  • @slimr/mdi-paths - A basic Icon component and Material Design icon svg paths, code-split by path.
  • @slimr/router - A novel React-web router that supports stack routing
  • @slimr/styled - css-in-js features inspired by the popular styled-components and Chakra-UI libs
  • @slimr/swr - A React hook for fetching data that supports stale-while-refresh eager rendering
  • @slimr/util - Framework agnostic Javascript polyfills

Setup

  • Install using normal methods (npm i, yarn i, ...etc)
  • There is a known conflict with vitest, which you can resolve by adding the following to vite.config.js
export default defineConfig({
  test: {
    deps: {
      inline: ['@slimr/hooks'],
    },
  },
})

API

npm:react-use

All of the hooks from npm:react-use, which are excellent

useDeepCompareMemo and useShallowCompareMemo

like react-use's useDeepEffects, but for memos

useForm, FormError

A hook and custom Error from @slimr/forms, which returns a Form component and reactive form state.

import {FormError, useForm} from '@slimr/forms'
import {formToValues} from '@slimr/util'

function MyForm() {
  const { Form, submitting, submitted, accepted, errors} = useForm()

  const onSubmit = async (e: React.FormEventHandler<HTMLFormElement> => {
    const vals = formToJson(e.target as HTMLFormElement)
    const errors: Record<string, string> = {}
    if (!vals.name) {
      errors.name = 'Name is required'
    }
    if (!vals.terms) {
      errors.checkbox = 'You must agree to the terms'
    }
    if (Object.keys(errors).length) {
      throw new FormError(errors)
    }
  }

  return (
    <Form onSubmit={onSubmit}>
      <input disabled={submitting || accepted} name="name" />
      <div>{errors.name}<div>
      <input disabled={submitting || accepted} name="terms" type="checkbox" />
      <div>{errors.terms}<div>
      <button type="submit">Submit</button>
      <button type="reset">Reset</button>
    </Form>
  )
}

useSet2

Returns a set-like object that intercepts the setter function to trigger re-renders on change. Also adds a toggle and reset method. @slimr/hooks also exports a useSet from react-use, which is similar but has a different, less desirable (imho) pattern.

function MyComponent() {
  const optionalInitialValue = new Set()
  const [set1, set1Setters] = useSet(optionalInitialValue)
  const set2 = useSet2(optionalInitialValue)

  // Use set2 like you would a vanilla JS Set

useSWR

A hook that accepts a function callback, calls the function and returns a reactive callback state. Uses a cache and will return the cache value if available while waiting for the callback to complete, then update the return on complete. This is often called 'stale-while-refresh' and abbreviated as 'SWR', hence the name of the hook. Source is in @slimr/swr

import {useSWR} from `@slimr/swr`

function MyComponent({ page }: number) {
  const { result, loading, refresh} = useSWR(() => getPageData(page), [page], {throttle: Infinity})
  if (loading) return null
  return (
    <section>
      <h1>{result.title}</h1>
      <p>{result.description}</h1>
      <button onClick={refresh}>Refresh</button>
    </section>
  )
}
2.1.45

8 months ago

2.1.43

8 months ago

2.1.44

8 months ago

2.1.40

8 months ago

2.1.38

8 months ago

2.1.39

8 months ago

2.1.36

9 months ago

2.1.37

8 months ago

2.1.34

9 months ago

2.1.35

9 months ago

2.1.33

9 months ago

2.1.32

1 year ago

2.1.31

1 year ago

2.1.30

1 year ago

2.1.29

1 year ago

2.1.28

1 year ago

2.1.27

1 year ago

2.1.26

1 year ago

2.1.25

1 year ago

2.1.24

1 year ago

2.1.23

1 year ago

2.1.22

1 year ago

2.1.21

1 year ago

2.1.20

1 year ago

2.1.18

1 year ago

2.1.17

1 year ago

2.1.16

1 year ago

2.1.15

1 year ago

2.1.14

1 year ago

2.1.13

1 year ago

2.1.12

1 year ago

2.1.11

1 year ago

2.1.10

1 year ago

2.1.9

1 year ago

2.1.7

1 year ago

2.1.6

1 year ago

2.1.5

1 year ago

1.0.2

1 year ago

1.0.1

1 year ago