2021-04-29 02:28:33 +00:00
|
|
|
import { useCallback, useState } from "react"
|
|
|
|
|
|
|
|
|
|
|
|
export default function useArrayState(def) {
|
|
|
|
const [value, setValue] = useState(def ?? [])
|
|
|
|
|
|
|
|
const appendValue = useCallback(
|
|
|
|
newSingle => {
|
|
|
|
setValue(
|
|
|
|
oldArray => [...oldArray, newSingle]
|
|
|
|
)
|
|
|
|
},
|
2021-04-29 03:03:34 +00:00
|
|
|
[]
|
2021-04-29 02:28:33 +00:00
|
|
|
)
|
|
|
|
|
2021-04-29 03:03:34 +00:00
|
|
|
const spliceValue = useCallback(
|
2021-04-29 02:28:33 +00:00
|
|
|
position => {
|
|
|
|
setValue(
|
|
|
|
oldArray => {
|
|
|
|
// TODO: Hope this doesn't break anything...
|
|
|
|
oldArray.splice(position, 1)
|
|
|
|
return oldArray
|
|
|
|
}
|
|
|
|
)
|
2021-04-29 03:03:34 +00:00
|
|
|
},
|
|
|
|
[]
|
|
|
|
)
|
|
|
|
|
|
|
|
const removeValue = useCallback(
|
|
|
|
remValue => {
|
|
|
|
setValue(
|
|
|
|
oldArray => oldArray.filter(item => item !== remValue)
|
|
|
|
)
|
|
|
|
},
|
|
|
|
[]
|
2021-04-29 02:28:33 +00:00
|
|
|
)
|
|
|
|
|
2021-04-29 03:03:34 +00:00
|
|
|
return {value, setValue, appendValue, spliceValue, removeValue}
|
2021-04-29 02:28:33 +00:00
|
|
|
}
|