Skip to content

Latest commit

 

History

History
65 lines (49 loc) · 2.34 KB

PREVENT_ACTIONS.md

File metadata and controls

65 lines (49 loc) · 2.34 KB

<- to the main page

Preventing actions

You can prevent actions from firing too often. For example, from sending requests to the server every time, if it was successfully sent once.

Just pass { canRepeatIn: 1000 } to createAction, where number is the time in ms, after which function is allowed to fire again. 0 means it can be fired only once.

// actions.js

import { createAction } from 'master-hook'

export const myAction = createAction(() => {
  // Do your stuff here
}, { canRepeatIn: 1000 })

You can just pass a number as second (or third) argument to createAction, it will also work

createAction(() => {}, 1000)

Force an action to fire

Sometimes you need to force an action to fire. Wrap your action in force function then and it wont pay attention to canRepeatIn.

// component.jsx

import React from 'react'
import { useMyHook } from './hooks.js'
import { force } from 'master-hook'

export const Component = () => {
  const {value, myAction} = useMyHook()

  function handleClick() {
    force(myAction())
  }

  return (
    <div onClick={handleClick}>
      {value}
    </div>
  )
}

See more: