use-undoable
Advanced tools
Comparing version 3.3.8 to 3.3.9
@@ -52,3 +52,4 @@ var react = require('react'); | ||
ignoreIdenticalMutations = action.ignoreIdenticalMutations, | ||
cloneState = action.cloneState; | ||
cloneState = action.cloneState, | ||
ignoreAction = action.ignoreAction; | ||
@@ -59,2 +60,10 @@ if (!payload) { | ||
if (ignoreAction) { | ||
return { | ||
past: past, | ||
present: payload, | ||
future: future | ||
}; | ||
} | ||
var mPast = [].concat(past); | ||
@@ -230,10 +239,11 @@ | ||
}, []); | ||
var update = react.useCallback(function (payload, mutationBehavior) { | ||
var update = react.useCallback(function (payload, mutationBehavior, ignoreAction) { | ||
return dispatch(_extends({ | ||
type: 'update', | ||
payload: payload, | ||
behavior: mutationBehavior | ||
behavior: mutationBehavior, | ||
ignoreAction: ignoreAction | ||
}, compileMutateOptions(options))); | ||
}, []); | ||
var setState = react.useCallback(function (payload, mutationBehavior) { | ||
var setState = react.useCallback(function (payload, mutationBehavior, ignoreAction) { | ||
if (mutationBehavior === void 0) { | ||
@@ -243,5 +253,9 @@ mutationBehavior = options.behavior; | ||
return typeof payload === 'function' ? update(payload(state.present), mutationBehavior) : update(payload, mutationBehavior); | ||
if (ignoreAction === void 0) { | ||
ignoreAction = false; | ||
} | ||
return typeof payload === 'function' ? update(payload(state.present), mutationBehavior, ignoreAction) : update(payload, mutationBehavior, ignoreAction); | ||
}, [state]); | ||
var static_setState = react.useCallback(function (payload, mutationBehavior) { | ||
var static_setState = react.useCallback(function (payload, mutationBehavior, ignoreAction) { | ||
if (mutationBehavior === void 0) { | ||
@@ -251,3 +265,7 @@ mutationBehavior = options.behavior; | ||
update(payload, mutationBehavior); | ||
if (ignoreAction === void 0) { | ||
ignoreAction = false; | ||
} | ||
update(payload, mutationBehavior, ignoreAction); | ||
}, []); | ||
@@ -254,0 +272,0 @@ return [state.present, setState, { |
@@ -52,3 +52,4 @@ import { useReducer, useCallback } from 'react'; | ||
ignoreIdenticalMutations = action.ignoreIdenticalMutations, | ||
cloneState = action.cloneState; | ||
cloneState = action.cloneState, | ||
ignoreAction = action.ignoreAction; | ||
@@ -59,2 +60,10 @@ if (!payload) { | ||
if (ignoreAction) { | ||
return { | ||
past: past, | ||
present: payload, | ||
future: future | ||
}; | ||
} | ||
var mPast = [].concat(past); | ||
@@ -230,10 +239,11 @@ | ||
}, []); | ||
var update = useCallback(function (payload, mutationBehavior) { | ||
var update = useCallback(function (payload, mutationBehavior, ignoreAction) { | ||
return dispatch(_extends({ | ||
type: 'update', | ||
payload: payload, | ||
behavior: mutationBehavior | ||
behavior: mutationBehavior, | ||
ignoreAction: ignoreAction | ||
}, compileMutateOptions(options))); | ||
}, []); | ||
var setState = useCallback(function (payload, mutationBehavior) { | ||
var setState = useCallback(function (payload, mutationBehavior, ignoreAction) { | ||
if (mutationBehavior === void 0) { | ||
@@ -243,5 +253,9 @@ mutationBehavior = options.behavior; | ||
return typeof payload === 'function' ? update(payload(state.present), mutationBehavior) : update(payload, mutationBehavior); | ||
if (ignoreAction === void 0) { | ||
ignoreAction = false; | ||
} | ||
return typeof payload === 'function' ? update(payload(state.present), mutationBehavior, ignoreAction) : update(payload, mutationBehavior, ignoreAction); | ||
}, [state]); | ||
var static_setState = useCallback(function (payload, mutationBehavior) { | ||
var static_setState = useCallback(function (payload, mutationBehavior, ignoreAction) { | ||
if (mutationBehavior === void 0) { | ||
@@ -251,3 +265,7 @@ mutationBehavior = options.behavior; | ||
update(payload, mutationBehavior); | ||
if (ignoreAction === void 0) { | ||
ignoreAction = false; | ||
} | ||
update(payload, mutationBehavior, ignoreAction); | ||
}, []); | ||
@@ -254,0 +272,0 @@ return [state.present, setState, { |
@@ -11,2 +11,3 @@ declare type ActionType = 'undo' | 'redo' | 'update' | 'reset' | 'resetInitialState'; | ||
cloneState?: boolean; | ||
ignoreAction?: boolean; | ||
} | ||
@@ -13,0 +14,0 @@ interface State { |
{ | ||
"name": "use-undoable", | ||
"version": "3.3.8", | ||
"version": "3.3.9", | ||
"description": "React hook for undo/redo functionality without the hassle.", | ||
@@ -5,0 +5,0 @@ "private": false, |
@@ -65,4 +65,8 @@ # useUndoable | ||
This README is the entire documentation. It is written such that you are meant to read it from top to bottom, without skipping around. You see, this project is, in a general sense, quite simple. It is imperative, however, that you have a deeper understanding of how it works. Having this understanding will allow you to make better decisions about the options and behaviors this package offers. | ||
useUndoable is one of those projects that doesn't have a massive layer of abstraction. In comparison to a project like, say, React, _how_ you use the program greatly differs from how it is built. | ||
This project is slightly different. Instead of just going through the API in a superficial way, I will actually teach you, more or less, how useUndoable works under-the-hood. You see, this project is, in a general sense, quite simple. It is imperative, however, that you have a deeper understanding of how it works. Having this understanding will allow you to make better decisions about the options and behaviors this package offers. | ||
This README is the entire documentation. It is written such that you are meant to read it from top to bottom, without skipping around. | ||
The best way to visualize how the useUndoable state system works is to use the [live demo](https://codesandbox.io/s/use-undoable-zi0b4) as a companion to this README. Simply open it up and make the state there match the examples offered below. | ||
@@ -102,2 +106,20 @@ | ||
The `setState` function accepts, in total, 3 parameters: | ||
- The state you want to set or a function to set it | ||
- The mutation behavior of this one `setState` call (optional) | ||
- The `ignoreAction` boolean (optional) | ||
The mutation behavior is described below. Normally, the mutation behavior is a global value, but you can alter it within individual state updates if you want. | ||
The `ignoreAction` boolean indicates whether or not to update the `past` and `future` of useUndoable's internal state. Essentially, if you set this to `true`, when you make a state update with `setState(c + 1)`, it will **only** update the **present** state. In other words, it will act like the normal `useState` hook. | ||
If you want to use the global mutation behavior and set the `ignoreAction` bool, set mutation behavior to `null`: | ||
```js | ||
const [yourState, setYourState, { undo, redo }] = useUndoable(); | ||
setYourState(yourState + 1, null, true); | ||
``` | ||
Heads up: Are you pulling data from an API? Stick around to read how to handle that properly with useUndoable. | ||
@@ -269,2 +291,3 @@ | ||
reset, | ||
static_setState, | ||
}, | ||
@@ -342,2 +365,10 @@ ] = useUndoable(initialState, options); | ||
### `static_setState` | ||
In some rare cases, you may run into the issue that the default `setState` function changes with every state change. This is, more or less, by design. | ||
One key behavior of the default `setState` is that you can pass either a value or a function. The function receives the present state as a parameter. As such, the function itself needs to change whenever the present state changes. | ||
If you have an issue with this, you can use the `static_setState` function. This **does not** accept a function, only a new value. This means it only needs to be created once. | ||
## Performance considerations | ||
@@ -344,0 +375,0 @@ |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
66693
532
450