Created
January 15, 2019 21:47
-
-
Save aprehot/af0b6b4555164073f4bba1c6297b37fb to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import {useState, useReducer} from 'react'; | |
// also add immer | |
const DEFAULT = 'DEFAULT'; | |
export const ADD_MARKER_FEATURE = 'ADD_MARKER_FEATURE'; | |
export const CREATE_FEATURES_LAYER = 'CREATE_FEATURES_LAYER'; | |
export function useMutation(initialValue) { | |
const [val, updateValue] = useState(initialValue); | |
return [ | |
val, | |
(updater) => { | |
updateValue(updater); | |
} | |
]; | |
} | |
export function useMutableReducer(reducer, initialState) { | |
return useReducer(reducer, initialState); | |
} | |
export function mutableReducer(draft, action) { | |
const reducer = { | |
ADD_MARKER_FEATURE: () => draft.push(action.markerFeature), | |
DEFAULT: () => null | |
}; | |
return (reducer[action.type] || reducer[DEFAULT])(); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment