Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
async function handleSubmit(event) {
event.preventDefault()
// mutate current data to optimistically update the UI
// the fetch below could fail, in that case the UI will
// be in an incorrect state
mutate('/api/data', [...data, text], false)
// send text to the API
await fetch('/api/data', {
method: 'POST',
body: JSON.stringify({ text })
})
// to revalidate the data and ensure is not incorrect
// we trigger a revalidation of the data
trigger('/api/data')
setText('')
}