简体   繁体   English

React Native 找不到已经声明的变量函数

[英]React Native can't find variable function that is already declared

I have a small app with FlatList buttons that launch a function outside of the render in a Functional Component.我有一个带有 FlatList 按钮的小应用程序,可以在功能组件中的渲染之外启动一个函数。 Race.js and Class.js both function JUST FINE. Race.js 和 Class.js 都可以正常运行。 Background.js was not sending the value from the button up to the parent (App.js) as its sibling components were, which is strange... I have copied the EXACT SAME functions and passed the same props for/to all three components, but just changed the names so they point to the relevant components (race, class, background - its a d&d app :P). Background.js 没有像它的兄弟组件那样将值从按钮发送到父级(App.js),这很奇怪......我复制了完全相同的函数并将相同的道具传递给/给所有三个组件,但只是更改了名称,以便它们指向相关组件(种族、类、背景 - 它是一个 d&d 应用程序:P)。

Can anyone see something that could be wrong with why one component sees the function and the next doesn't?任何人都可以看到为什么一个组件可以看到该功能而下一个组件没有的问题?

The problem is Race.js/Class.js work and 'selectRace' is found and called properly:问题是 Race.js/Class.js 工作并且找到并正确调用了“selectRace”:

import React, { useState } from "react";
import { SafeAreaView, StyleSheet, Text, View } from "react-native";
import { FlatList, TouchableOpacity } from "react-native-gesture-handler";
import { Races } from "../components/races";

const Race = (props) => {
  const razas = props.raceNames;

  selectRace = (race) => {
    console.log("race button pushed: ", race);

    const index = (element) => element.name === race;
    let raceIndex = Races.findIndex(index);
    props.setRace(raceIndex);
  };

  return (
    <SafeAreaView style={styles.container}>
      <View style={styles.main}>
        <Text style={styles.titulo}>Elige una Raza:</Text>

        <FlatList
          numColumns={2}
          data={razas}
          renderItem={({ item }) => (
            <View style={styles.gridWrapper} key={item.id}>
              <TouchableOpacity
                style={styles.button}
                onPress={() => {
                  selectRace(`${item.name}`);
                  props.navigation.navigate("Class");
                }}>
                <Text style={styles.text}>{item.name}</Text>
              </TouchableOpacity>
            </View>
          )}
        />
      </View>
    </SafeAreaView>
  );
};

export default Race;

but in Background.js, the SAME EXACT SYNTAX fails and upon rendering the navigation route, it throws an error saying it cannot find the variable "selectBackground" even though it's OBVIOUSLY declared right in the function, the same as the previous two components:但是在 Background.js 中,SAME EXACT SYNTAX 失败,并且在呈现导航路线时,它抛出一个错误,指出它找不到变量“selectBackground”,即使它在函数中明显声明,与前两个组件相同:

import React, { useState } from "react";
import { SafeAreaView, StyleSheet, Text, View } from "react-native";
import { FlatList, TouchableOpacity } from "react-native-gesture-handler";
import { Backgrounds } from "../components/backgrounds";

const Background = (props) => {
  const backgroundNames = props.backgroundNames;

  selectBackground = (b) => {
    console.log("Background button pushed: ", b);

    const index = (element) => element.name === b;
    let backgroundIndex = Backgrounds.findIndex(index);
    props.setBackground(backgroundIndex);
  };


  return (
    <SafeAreaView style={styles.container}>
      <View style={styles.main}>
        <Text style={styles.titulo}>Elige un Trasfondo:</Text>

        <FlatList
          numColumns={2}
          data={backgroundNames}
          renderItem={({ item }) => (
            <View style={styles.gridWrapper} key={item.id}>
              <TouchableOpacity
                style={styles.button}
                onPress={() => {
                  selectBackground(`${item.name}`);
                  // props.navigation.navigate("Imagen");
                }}>
                <Text style={styles.text}>{item.name}</Text>
              </TouchableOpacity>
            </View>
          )}
        />
      </View>
    </SafeAreaView>
  );
};

export default Background;

What's more frustrating is that before, it was finding it just fine but it simply wasn't hoisting data up to the parent in "props.setBackground(index)" and now it cannot find this "variable" at all...The only thing I change was the name of the prop being passed in so that it wasn't the same as the function itself in the child, just in case having the function called 'selectBackground' and 'props.selectBackground' could possibly cause some kind of issue (I don't leave anything to chance with computers anymore...).更令人沮丧的是,之前它发现它很好,但它根本没有将数据提升到“props.setBackground(index)”中的父级,现在它根本找不到这个“变量”......唯一的我更改的是传入的道具的名称,因此它与子项中的函数本身不同,以防万一具有名为“selectBackground”和“props.selectBackground”的函数可能会导致某种问题(我不再给计算机留下任何机会......)。

I have also tried lifting it outside the actual component rendering area { const Background = (props) ... } and put the function under the imports as "const selectBackground..." and now my button can find it, and it launched the console.log inside, but then failed when it hit the final line in the function of "props.setBackground()" because obviously it's outside the component receiving the props, and I cannot receive/set props outside there.我还尝试将它提升到实际组件渲染区域之外 { const Background = (props) ... } 并将该函数放在导入下作为“const selectBackground ...”,现在我的按钮可以找到它,并且它启动了console.log 在里面,但是当它到达“props.setBackground()”函数的最后一行时失败了,因为显然它在接收道具的组件之外,我无法在那里接收/设置道具。 Why is it doing this?!它为什么要这样做?!

To make it work you first need to declare your selectBackground function with "const" like:要使其工作,您首先需要使用“const”声明您的 selectBackground 函数,例如:

const selectBackground = (b) =>

and your code will look like:您的代码将如下所示:

const Background = (props) => {
  const backgroundNames = props.backgroundNames;

  const selectBackground = (b) => {
    console.log("Background button pushed: ", b);

    const index = (element) => element.name === b;
    let backgroundIndex = Backgrounds.findIndex(index);
    props.setBackground(backgroundIndex);
  };

Second, you can call your function in the way I mentioned the code below:其次,您可以按照我在下面提到的代码的方式调用您的函数:

const callFunction = (value) => {
    console.log(value)
}

<SafeAreaView setBackground={callFunction} />

You need to pass a function to a component without parentheses and then inside a component, you will call it with parentheses.您需要将函数传递给不带括号的组件,然后在组件内部,您将使用括号调用它。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM