You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
25 lines
538 B
JavaScript
25 lines
538 B
JavaScript
5 years ago
|
import {useReducer} from 'react'
|
||
|
|
||
|
const errorReducer = (errors, action) => {
|
||
|
switch (action.type) {
|
||
|
case 'set_errors':
|
||
|
const newErrors = {}
|
||
|
for(const field of action.errors)
|
||
|
newErrors[field.param] = field.msg
|
||
|
return newErrors
|
||
|
|
||
|
case 'clear_error':
|
||
|
console.log('clear_error', action)
|
||
|
return {
|
||
|
...errors,
|
||
|
[action.field]: undefined
|
||
|
}
|
||
|
|
||
|
default:
|
||
|
return errors
|
||
|
}
|
||
|
}
|
||
|
|
||
|
export default function useErrorReducer(initialState = {}){
|
||
|
return useReducer( errorReducer )
|
||
|
}
|