Al presionar un botón, necesito pasar parámetros a otra pantalla donde se obtiene el valor de ese botón, código a continuación;
pantalla: 1
<TouchableOpacity onPress={() => navigation.navigate("Quiz", { fetch: "history" })} style={styles.button} > <Text style={styles.buttonText}>History</Text> </TouchableOpacity>
pantalla: 2
const Quiz = ({ navigation, route }) => { const { fetch } = route.params; const getQuiz = async () => { setIsLoading(true); const url = `https://herokuapp.com/q/${fetch}`; const res = await fetch(url); const data = await res.json(); setQuestions(data.results); setOptions(generateOptionsAndShuffle(data.results[0])); setIsLoading(false); };
Pero durante la búsqueda me sale el siguiente error
[Unhandled promise rejection: TypeError: fetch is not a function. (In 'fetch(url)', 'fetch' is "history")]
He intentado usar el tiempo de espera pero no funciona, ¿hay alguna opción mejor?
El problema es que fetch
es una función nativa en javascript (ver aquí ).
Debe cambiar el nombre del parámetro a otro nombre como quizz
.
pantalla: 1
<TouchableOpacity onPress={() => navigation.navigate("Quiz", { quizz: "history" })} style={styles.button} > <Text style={styles.buttonText}>History</Text> </TouchableOpacity>
pantalla: 2
const Quiz = ({ navigation, route }) => { const { quizz } = route.params; const getQuiz = async () => { setIsLoading(true); const url = `https://herokuapp.com/q/${quizz}`; const res = await fetch(url); const data = await res.json(); setQuestions(data.results); setOptions(generateOptionsAndShuffle(data.results[0])); setIsLoading(false); };