On Press of a button I need to pass params to another screen where value of that button is fetched, code below;
screen:1
JavaScript
x
8
1
<TouchableOpacity
2
onPress={() => navigation.navigate("Quiz", { fetch: "history" })}
3
style={styles.button}
4
>
5
<Text style={styles.buttonText}>History</Text>
6
7
</TouchableOpacity>
8
screen:2
JavaScript
1
13
13
1
const Quiz = ({ navigation, route }) => {
2
const { fetch } = route.params;
3
4
const getQuiz = async () => {
5
setIsLoading(true);
6
const url = `https://herokuapp.com/q/${fetch}`;
7
const res = await fetch(url);
8
const data = await res.json();
9
setQuestions(data.results);
10
setOptions(generateOptionsAndShuffle(data.results[0]));
11
setIsLoading(false);
12
};
13
But during the fetching I get the following error
JavaScript
1
2
1
[Unhandled promise rejection: TypeError: fetch is not a function. (In 'fetch(url)', 'fetch' is "history")]
2
I have tried using timeout but that is not working, is there a better option.
Advertisement
Answer
The issue is that fetch
is a native function in javascript (see here).
You should rename the param to another name like quizz
.
screen:1
JavaScript
1
7
1
<TouchableOpacity
2
onPress={() => navigation.navigate("Quiz", { quizz: "history" })}
3
style={styles.button}
4
>
5
<Text style={styles.buttonText}>History</Text>
6
</TouchableOpacity>
7
screen:2
JavaScript
1
13
13
1
const Quiz = ({ navigation, route }) => {
2
const { quizz } = route.params;
3
4
const getQuiz = async () => {
5
setIsLoading(true);
6
const url = `https://herokuapp.com/q/${quizz}`;
7
const res = await fetch(url);
8
const data = await res.json();
9
setQuestions(data.results);
10
setOptions(generateOptionsAndShuffle(data.results[0]));
11
setIsLoading(false);
12
};
13