React Native – 作为参数传递的函数未定义

huangapple go评论50阅读模式
英文:

React Native - Funtion passed as a paramater is undefined

问题

以下是翻译的代码部分,不包括问题的回答:

App.js

import React, { useState } from 'react';
import { View, Text, StyleSheet, FlatList, Alert } from 'react-native';
import { NavigationContainer } from '@react-navigation/native';
import { createBottomTabNavigator } from '@react-navigation/bottom-tabs';
import { v4 as uuid } from 'uuid';
import 'react-native-get-random-values';

import Team from './pages/Team';
import Header from './components/Header';
import Home from './pages/Home';

import axios from 'axios';

const App = () => {
  const [teamData, setTeamData] = useState([]);
  const [confIndex, setConfIndex] = useState();

  function SettingsScreen() {
    return (
      <View style={{ flex: 1, justifyContent: 'center', alignItems: 'center' }}>
        <Text>Settings!</Text>
      </View>
    );
  }

  const Tab = createBottomTabNavigator();

  const confOnClick = async text => {
    console.log(text);
    if (text === 'East') {
      setConfIndex(0);
    } else {
      setConfIndex(1);
    }
    const options = {
      method: 'GET',
      url: 'https://api-nba-v1.p.rapidapi.com/teams',
      params: { conference: text },
      headers: {
        'X-RapidAPI-Key': 'your-api-key',
        'X-RapidAPI-Host': 'api-nba-v1.p.rapidapi.com',
      },
    };
    await axios
      .request(options)
      .then(function (response) {
        console.log(response.data.response);
        console.log(typeof response.data.response);
        setTeamData(response.data.response);
      })
      .catch(function (error) {
        console.error(error);
      });
  };

  return (
    <View style={styles.container}>
      <Header />
      <NavigationContainer>
        <Tab.Navigator screenOptions={{ headerShown: false }}>
          <Tab.Screen name="Home" component={Home} />
          <Tab.Screen
            name="Team"
            component={Team}
            options={{ confOnClick: confOnClick, teamData: teamData }}
          />
          <Tab.Screen name="Settings" component={SettingsScreen} />
        </Tab.Navigator>
      </NavigationContainer>
    </View>
  );
};

const styles = StyleSheet.create({
  container: {
    flex: 1,
  },
});

export default App;

Team.js

import React from 'react';
import { View, StyleSheet, FlatList, Alert } from 'react-native';
import ListItem from '../components/ListItem';
import Category from '../components/Category';

function Team({ confOnClick, teamData }) {
  return (
    <View>
      <Category confOnClick={confOnClick} />
      <FlatList
        data={teamData}
        renderItem={({ item }) => <ListItem item={item} />}
      />
    </View>
  );
}
export default Team;

Category.js

import React from 'react';
import { View, StyleSheet, FlatList, Alert } from 'react-native';
import ListItem from '../components/ListItem';
import Category from '../components/Category';

function Team({ confOnClick, teamData }) {
  return (
    <View>
      <Category confOnClick={confOnClick} />
      <FlatList
        data={teamData}
        renderItem={({ item }) => <ListItem item={item} />}
      />
    </View>
  );
}
export default Team;

上面是你提供的代码的翻译,如果你需要进一步的帮助或有其他问题,请告诉我。

英文:

I am new to React Native and I am trying to create a simple app with tab navigation to fetch NBA data. I created a Team page with 2 buttons to fetch East or West conference team data. I tried to pass all the params from App.js to Team.js. It was meant to be fetching the data from the API when button is clicked.

However, an error occured when i tried to press the button in Team (the two buttons in Category component)
Error: Category.js:13:70 TypeError: undefined is not a function, js engine: hermes) ... a function (confOncClick) that was used in the Category.js (A component in Team page) is undefined

Error screenshot: Error

App.js

import React, {useState} from &#39;react&#39;;
import {View, Text, StyleSheet, FlatList, Alert} from &#39;react-native&#39;;
import {NavigationContainer} from &#39;@react-navigation/native&#39;;
import {createBottomTabNavigator} from &#39;@react-navigation/bottom-tabs&#39;;
import {v4 as uuid} from &#39;uuid&#39;;
import &#39;react-native-get-random-values&#39;;
import Team from &#39;./pages/Team&#39;;
import Header from &#39;./components/Header&#39;;
import Home from &#39;./pages/Home&#39;;
import axios from &#39;axios&#39;;
const App = () =&gt; {
const [teamData, setTeamData] = useState([]);
const [confIndex, setConfIndex] = useState();
function SettingsScreen() {
return (
&lt;View style={{flex: 1, justifyContent: &#39;center&#39;, alignItems: &#39;center&#39;}}&gt;
&lt;Text&gt;Settings!&lt;/Text&gt;
&lt;/View&gt;
);
}
const Tab = createBottomTabNavigator();
const confOnClick = async text =&gt; {
console.log(text);
if (text === &#39;East&#39;) {
setConfIndex(0);
} else {
setConfIndex(1);
}
const options = {
method: &#39;GET&#39;,
url: &#39;https://api-nba-v1.p.rapidapi.com/teams&#39;,
params: {conference: text},
headers: {
&#39;X-RapidAPI-Key&#39;: /.../,
&#39;X-RapidAPI-Host&#39;: &#39;api-nba-v1.p.rapidapi.com&#39;,
},
};
await axios
.request(options)
.then(function (response) {
console.log(response.data.response);
console.log(typeof response.data.response);
setTeamData(response.data.response);
})
.catch(function (error) {
console.error(error);
});
};
return (
&lt;View style={styles.container}&gt;
&lt;Header /&gt;
&lt;NavigationContainer&gt;
&lt;Tab.Navigator screenOptions={{headerShown: false}}&gt;
&lt;Tab.Screen name=&quot;Home&quot; component={Home} /&gt;
&lt;Tab.Screen
name=&quot;Team&quot;
component={Team}
options={{confOnClick: confOnClick, teamData: teamData}}
/&gt;
&lt;Tab.Screen name=&quot;Settings&quot; component={SettingsScreen} /&gt;
&lt;/Tab.Navigator&gt;
&lt;/NavigationContainer&gt;
&lt;/View&gt;
);
};
const styles = StyleSheet.create({
container: {
flex: 1,
},
});
export default App;

Team.js

import React from &#39;react&#39;;
import {View, StyleSheet, FlatList, Alert} from &#39;react-native&#39;;
import ListItem from &#39;../components/ListItem&#39;;
import Category from &#39;../components/Category&#39;;
function Team({confOnClick, teamData}) {
return (
&lt;View&gt;
&lt;Category confOnClick={confOnClick} /&gt;
&lt;FlatList
data={teamData}
renderItem={({item}) =&gt; &lt;ListItem item={item} /&gt;}
/&gt;
&lt;/View&gt;
);
}
export default Team;

Category.js

import React from &#39;react&#39;;
import {View, StyleSheet, FlatList, Alert} from &#39;react-native&#39;;
import ListItem from &#39;../components/ListItem&#39;;
import Category from &#39;../components/Category&#39;;
function Team({confOnClick, teamData}) {
return (
&lt;View&gt;
&lt;Category confOnClick={confOnClick} /&gt;
&lt;FlatList
data={teamData}
renderItem={({item}) =&gt; &lt;ListItem item={item} /&gt;}
/&gt;
&lt;/View&gt;
);
}
export default Team;

the confOnClick passed to Category seems to be undefined but I am not sure what is the issue. Could be because of the way I pass from app.js?

&lt;Tab.Screen
name=&quot;Team&quot;
component={Team}
options={{confOnClick: confOnClick, teamData: teamData}}
/&gt;

hope I made it clear and please let me know if I can provide more info

I have tried passing the params in different ways in Tab.Screen navigator with initialParams, params, options and also change the component to children but somehow i didn't get it right.

答案1

得分: 0

在Tab.Screen中,您可以使用initialParams,如下所示:

<Tab.Screen
  name="Team"
  component={Team}
  initialParams={{ confOnClick: confOnClick, teamData: teamData }}
/>

在Team.js中,您可以获取这些参数,如下所示:

function Team({ route }) {
  const { confOnClick, teamData } = route.params;
  // ...
}
英文:

you can use initialParams in Tab.Screen like

&lt;Tab.Screen
name=&quot;Team&quot;
component={Team}
initialParams={{confOnClick: confOnClick, teamData: teamData}}
/&gt;

Team.js

function Team({route}) {
const {confOnClick, teamData} = route.params;
...
}

huangapple
  • 本文由 发表于 2023年2月10日 09:59:40
  • 转载请务必保留本文链接:https://go.coder-hub.com/75406255.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定