[英]fetching and passing params in react navigation
On Press of a button I need to pass params to another screen where value of that button is fetched, code below;在按下按钮时,我需要将参数传递到另一个屏幕,在该屏幕中获取该按钮的值,代码如下;
screen:1屏幕:1
<TouchableOpacity
onPress={() => navigation.navigate("Quiz", { fetch: "history" })}
style={styles.button}
>
<Text style={styles.buttonText}>History</Text>
</TouchableOpacity>
screen:2屏幕: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);
};
But during the fetching I get the following error但是在获取过程中出现以下错误
[Unhandled promise rejection: TypeError: fetch is not a function. (In 'fetch(url)', 'fetch' is "history")]
I have tried using timeout but that is not working, is there a better option.我尝试过使用超时,但这不起作用,是否有更好的选择。
The issue is that fetch
is a native function in javascript (see here ).问题是fetch
是 javascript 中的本机函数(请参阅此处)。
You should rename the param to another name like quizz
.您应该将参数重命名为另一个名称,例如quizz
。
screen:1屏幕:1
<TouchableOpacity
onPress={() => navigation.navigate("Quiz", { quizz: "history" })}
style={styles.button}
>
<Text style={styles.buttonText}>History</Text>
</TouchableOpacity>
screen:2屏幕: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);
};
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.