Created
August 25, 2020 14:48
-
-
Save BrunoQuaresma/3ee3bd37a6635bafe7b8e6577ddc16f9 to your computer and use it in GitHub Desktop.
Form Initialize for React
This file contains 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
function initializeForm(form) { | |
return Object.keys(form).reduce((newForm, key) => { | |
let value | |
if(form[key] === null || form[key] === undefined) { | |
value = '' | |
} else if(typeof form[key] === 'string') { | |
value = form[key] | |
} else if(typeof form[key] === 'object') { | |
value = initializeForm(form[key]) | |
} | |
return { | |
...newForm, | |
[key]: value | |
} | |
}, {}) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment