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)
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>
)
}
- Using Storages
- Using Actions
- Using Selectors
- Caching
- Preventing Rerendering
- Preventing Actions [you are here]
- Autoset 'isPending'