Last active
June 29, 2020 14:47
-
-
Save jmaicaaan/3db8a1983f3d770ca7ba6daaac04bedd 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
// Updating the first approach (non schema-based) | |
const handleSubmit = async ({ | |
fname: firstName, | |
lname: lastName, | |
image: imageURL | |
}) => { | |
const response = await fetch('https://myserver.com/api/user', { | |
method: 'POST', | |
body: JSON.stringify({ | |
firstName, | |
lastName, | |
imageURL, | |
}), | |
}); | |
// ... | |
}; | |
// Schema-based using Yup | |
const schema = Yup.object().shape({ | |
firstName: Yup.string(), | |
lastName: Yup.string(), | |
avatarURL: Yup.string(), | |
}); | |
const handleSubmit = async (formValues) => { | |
const data = schema | |
.clone() | |
.from('fname', 'firstName') | |
.from('lname', 'lastName') | |
.from('image', 'avatarURL') | |
.cast(formValues); | |
const response = await fetch('https://myserver.com/api/user', { | |
method: 'POST', | |
body: JSON.stringify(data), | |
}); | |
// ... | |
}; | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment