clear(toNull = false)
create.clear
create(actionType).clear
Appropriate leaf state: any
Returns an (action) object that the reduxLeaves reducer uses to clears the leaf's state.
If toNull === true
, then it updates it to null
, otherwise it follows the type of the leaf's state:
- number clears to
0
- string clears to
''
- boolean clears to
false
- array clears to
[]
- object clears to
{}
Parameters
toNull
(boolean, optional): defaults tofalse
Returns
action
(object): an object to dispatch to the store
Example
import { createStore } from 'redux'
import reduxLeaves from 'reduxLeaves'
const initialState = {
bool: true,
num: 2,
str: 'foo',
arr: [1, 2, 3]
}
const [reducer, actions] = reduxLeaves(initialState)
const store = createStore(reducer)
Boolean state
const clearBool = actions.bool.create.clear
store.dispatch(clearBool())
console.log(store.getState().bool) // false
store.dispatch(clearBool(true))
console.log(store.getState().bool) // null
Number state
const clearNum = actions.num.create.clear
store.dispatch(clearNum())
console.log(store.getState().num) // 0
store.dispatch(clearNum(true))
console.log(store.getState().num) // null
String state
const clearStr = actions.str.create.clear
store.dispatch(clearStr())
console.log(store.getState().str) // ''
store.dispatch(clearStr(true))
console.log(store.getState().str) // null
Array state
const clearArr = actions.arr.create.clear
store.dispatch(clearArr())
console.log(store.getState().arr) // []
store.dispatch(clearArr(true))
console.log(store.getState().arr) // null
Object state
const clearState = actions.create.clear
store.dispatch(clearState())
console.log(store.getState()) // {}
store.dispatch(clearState(true))
console.log(store.getState()) // null