Created
March 7, 2025 18:41
-
-
Save SalcidoJesus/bbd4c888243e63366ef59e758ed9a806 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
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