0.5.5 • Published 4 years ago

@tourepedia/react-hooks v0.5.5

Weekly downloads
1
License
MIT
Repository
github
Last release
4 years ago

React Hooks

Some helper react hooks

Install

npm install --save @tourepedia/react-hooks

Usage

useDidMount

Call a function on component's didMount Lifecycle

import { useDidMount } from "@tourepedia/react-hooks"

function App() {
  useDidMount(() => {
    // only called on mount
    fetchTheData()
  })
}

useDidUpdate

Call a function only in component's didUpdate Lifecycle.

import { useDidUpdate } from "@tourepedia/react-hooks"

function App({ count }) {
  useDidUpdate(() => {
    // called when the count changes
  }, [count])
}

useOnce

Only call a function once in a component's Lifecycle

import { useOnce } from "@tourepedia/react-hooks"

function App() {
  useOnce(() => {
    // only called once
    fetchTheData()
  })
}

useEnforceFocus

Enforces focus to be contained in a container and return back to last focused element based on a condition

useEnforceFocus(
  containerRef, // React.Ref: of the container
  shouldBeFocused, // boolean: (true) to toggle the focus enforcement
  config: {
    autoFocus // boolean: (true) should the container be autoFocus
    enforceFocus // boolean: (true) should return the focus to the last focused element
  }
)
import { useRef, useState } from "react"
import { useEnforceFocus } from "@tourepedia/react-hooks"

function App() {
  const containerRef = useRef()
  const [shouldBeFocused, toggleShouldFocus] = useState(false)
  // will focus the form container when shouldBeFocused is set to true
  useEnforceFocus(containerRef, shouldBeFocused)
  return (
    <div>
      <form className="container" ref={containerRef}>
        <input type="email" autoFocus />
        <button type="submit">Save</button>
      </form>
      <button onClick={() => toggleShouldFocus(!shouldBeFocused)}>
        Toggle Focus
      </button>
    </div>
  )
}
0.5.5

4 years ago

0.5.4

5 years ago

0.5.3

5 years ago

0.5.2

5 years ago

0.5.1

5 years ago

0.5.0

5 years ago

0.4.1

5 years ago

0.4.0

5 years ago

0.3.2

5 years ago

0.3.1

5 years ago

0.3.0

5 years ago

0.2.7

5 years ago

0.2.6

5 years ago

0.2.5

5 years ago

0.2.4

5 years ago

0.2.3

5 years ago

0.2.2

5 years ago

0.1.0

5 years ago