Skip to content

Instantly share code, notes, and snippets.

@SalcidoJesus
Created March 7, 2025 18:41
Show Gist options
  • Save SalcidoJesus/bbd4c888243e63366ef59e758ed9a806 to your computer and use it in GitHub Desktop.
Save SalcidoJesus/bbd4c888243e63366ef59e758ed9a806 to your computer and use it in GitHub Desktop.
import { View, Text } from 'react-native'
import { useState } from 'react'
import { Button, TextInput } from 'react-native-paper'
import { cliente_axios } from '../global/cliente_axios'
export default function formulario() {
const [titulo, setTitulo] = useState('')
const [descripcion, setDescripcion] = useState('')
const [errores, setErrores] = useState([])
async function guardar() {
setErrores([])
try {
await cliente_axios.post('/tareas', {
titulo,
descripcion
})
alert('Guardado')
setErrores([])
setTitulo('')
setDescripcion('')
} catch (error) {
if (error.request) {
console.log('error de validación');
console.log(error.response.data.errores);
setErrores(error.response.data.errores);
} else {
console.log(error)
alert('Error al guardar')
setErrores([])
}
}
}
return (
<View
style={{
flex: 1,
backgroundColor: 'white',
padding: 20,
gap: 20
}}
>
<Text>formulario</Text>
<TextInput
label='Título'
mode='outlined'
placeholder='Ingresa el título'
value={titulo}
onChangeText={ texto => setTitulo(texto) }
/>
<TextInput
label='Descripción'
mode='outlined'
multiline
numberOfLines={3}
placeholder='Ingresa la descripción'
value={descripcion}
onChangeText={ texto => setDescripcion(texto) }
/>
<Button mode="elevated" onPress={ guardar }>
Guardar
</Button>
{
errores.map((err, index) => (
<Text key={index}>{err}</Text>
))
}
</View>
)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment