React native 组件异常-Can';找不到变量:方向

React native 组件异常-Can';找不到变量:方向,react-native,React Native,我正在尝试在react中运行此应用程序,但它不断给我错误,无法找到变量方向。我该如何解决这个问题。代码如下 APP.js // Importing Libs import React, {useState} from 'react'; import { StyleSheet, View , SafeAreaView} from 'react-native'; import Header from './APP/Components/Header'; im

我正在尝试在react中运行此应用程序,但它不断给我错误,无法找到变量方向。我该如何解决这个问题。代码如下

APP.js

    // Importing Libs

    import React, {useState} from 'react';
    import { StyleSheet, View , SafeAreaView} from 'react-native';
    import Header from './APP/Components/Header';
    import StartGameScreen from './APP/Screens/StartGameScreen';
    import GameScreen from './APP/Screens/GameScreen';

    export default function App() {

    const [userNumber, setUserNumber] = useState();

    const startGameHandler = (selectedNumber) => {
      setUserNumber(selectedNumber);
    };

    let content = <StartGameScreen onStartGame={startGameHandler} />;

    if (userNumber) {
      content = <GameScreen userChoice={userNumber} />;
    }

      return (
        
        <View style={styles.screen}>
          <Header title="GUESS THE NUMBER" />
          {content}
        </View>
        
      );
    }

    const styles = StyleSheet.create({
     screen: {
      flex: 1
     },
    });
    import React, {useState, useRef} from 'react';
import {View , Text , StyleSheet, Button, Alert} from 'react-native';


const generateRandomBetween = (min, max , exclude) => {
    min = Math.ceil(min);
    max = Math.floor(max);
    const rndNum = Math.floor(Math.random() * (max - min)) + min;
    if (rndNum === exclude) {
        return generateRandomBetween(min, max, exclude);
    } else {
        return rndNum;
    }
};

const GameScreen = props => {
  const [currentGuess, setCurrentGuess] = useState(generateRandomBetween(1, 99, props.userChoice));

  const currentLow = useRef(1);
  const currentHigh = useRef(99);

  const nextGuessHandler = direction => {
    if ((direction === 'lower' && currentGuess < props.userChoice) || (direction === 'greater' && currentGuess > props.userChoice) ) {
      Alert.alert('Don\'t lie to me', 'You Can\'t Trick Me' [{text: 'I won\'t lie again , SORRY!', style: 'cancel'}]);
    }
    
  }

  if ( direction === 'lower') {
       currentHigh.current = currentGuess;
  } else {
       currentLow.current = currentGuess;
  }
 const nextNumber = generateRandomBetween(currentLow.current, currentHigh.current, currentGuess);
 currentGuess(nextNumber);



  return (
    <View style={styles.screen}>
    <Text>Opponent Guess</Text>
    <View style={styles.guessNumberContainer}>
      <Text style={styles.guessText}>{currentGuess}</Text>
    </View>
    <View style={styles.buttonContainer}>
       <Button title="LOWER" onPress={nextGuessHandler.bind(this, 'lower')} />
       <Button title="HIGHER" onPress={nextGuessHandler.bind(this, 'greater')} />
    </View>
    </View>
  )
};

const styles = StyleSheet.create({
  gameScreenContainer: {
    width: 300,
    maxWidth: '80%',
    alignItems: 'center',
    shadowColor: 'black',
    shadowOffset: { width: 0, height: 2},
    shadowRadius: 4,
    shadowOpacity: 0.26,
    backgroundColor:'#fff',
    elevation: 8 ,
    padding: 20,
    borderRadius: 15,
    marginTop: 30
  },

  guessNumberContainer:{
    borderWidth: 2,
    borderColor: "#FFC45D",
    padding: 10,
    borderRadius: 10,
    marginVertical: 10,
    alignItems: 'center',
    justifyContent:'center',
    width: 200
  },

  guessText:{
    color: "#FFC45D",
    fontSize: 22
  },

  screen:{
      flex: 1,
      padding: 10,
      alignItems: 'center'
  },

  buttonContainer:{
    width: 300,
    maxWidth: '80%',
    justifyContent: 'space-between',
    alignItems: 'center',
    shadowColor: 'black',
    shadowOffset: { width: 0, height: 2},
    shadowRadius: 4,
    shadowOpacity: 0.26,
    backgroundColor:'#fff',
    elevation: 8 ,
    padding: 20,
    borderRadius: 15,
    flexDirection: 'row',
    marginTop: 20
  }
});

export default GameScreen;
//导入Libs
从“React”导入React,{useState};
从“react native”导入{StyleSheet,View,SafeAreaView};
从“./APP/Components/Header”导入标题;
从“/APP/Screens/StartGameScreen”导入StartGameScreen;
从“./APP/Screens/GameScreen”导入游戏屏幕;
导出默认函数App(){
const[userNumber,setUserNumber]=useState();
常量startGameHandler=(selectedNumber)=>{
setUserNumber(selectedNumber);
};
让内容=;
如果(用户编号){
内容=;
}
返回(
{content}
);
}
const styles=StyleSheet.create({
屏幕:{
弹性:1
},
});
GameScreen.js

    // Importing Libs

    import React, {useState} from 'react';
    import { StyleSheet, View , SafeAreaView} from 'react-native';
    import Header from './APP/Components/Header';
    import StartGameScreen from './APP/Screens/StartGameScreen';
    import GameScreen from './APP/Screens/GameScreen';

    export default function App() {

    const [userNumber, setUserNumber] = useState();

    const startGameHandler = (selectedNumber) => {
      setUserNumber(selectedNumber);
    };

    let content = <StartGameScreen onStartGame={startGameHandler} />;

    if (userNumber) {
      content = <GameScreen userChoice={userNumber} />;
    }

      return (
        
        <View style={styles.screen}>
          <Header title="GUESS THE NUMBER" />
          {content}
        </View>
        
      );
    }

    const styles = StyleSheet.create({
     screen: {
      flex: 1
     },
    });
    import React, {useState, useRef} from 'react';
import {View , Text , StyleSheet, Button, Alert} from 'react-native';


const generateRandomBetween = (min, max , exclude) => {
    min = Math.ceil(min);
    max = Math.floor(max);
    const rndNum = Math.floor(Math.random() * (max - min)) + min;
    if (rndNum === exclude) {
        return generateRandomBetween(min, max, exclude);
    } else {
        return rndNum;
    }
};

const GameScreen = props => {
  const [currentGuess, setCurrentGuess] = useState(generateRandomBetween(1, 99, props.userChoice));

  const currentLow = useRef(1);
  const currentHigh = useRef(99);

  const nextGuessHandler = direction => {
    if ((direction === 'lower' && currentGuess < props.userChoice) || (direction === 'greater' && currentGuess > props.userChoice) ) {
      Alert.alert('Don\'t lie to me', 'You Can\'t Trick Me' [{text: 'I won\'t lie again , SORRY!', style: 'cancel'}]);
    }
    
  }

  if ( direction === 'lower') {
       currentHigh.current = currentGuess;
  } else {
       currentLow.current = currentGuess;
  }
 const nextNumber = generateRandomBetween(currentLow.current, currentHigh.current, currentGuess);
 currentGuess(nextNumber);



  return (
    <View style={styles.screen}>
    <Text>Opponent Guess</Text>
    <View style={styles.guessNumberContainer}>
      <Text style={styles.guessText}>{currentGuess}</Text>
    </View>
    <View style={styles.buttonContainer}>
       <Button title="LOWER" onPress={nextGuessHandler.bind(this, 'lower')} />
       <Button title="HIGHER" onPress={nextGuessHandler.bind(this, 'greater')} />
    </View>
    </View>
  )
};

const styles = StyleSheet.create({
  gameScreenContainer: {
    width: 300,
    maxWidth: '80%',
    alignItems: 'center',
    shadowColor: 'black',
    shadowOffset: { width: 0, height: 2},
    shadowRadius: 4,
    shadowOpacity: 0.26,
    backgroundColor:'#fff',
    elevation: 8 ,
    padding: 20,
    borderRadius: 15,
    marginTop: 30
  },

  guessNumberContainer:{
    borderWidth: 2,
    borderColor: "#FFC45D",
    padding: 10,
    borderRadius: 10,
    marginVertical: 10,
    alignItems: 'center',
    justifyContent:'center',
    width: 200
  },

  guessText:{
    color: "#FFC45D",
    fontSize: 22
  },

  screen:{
      flex: 1,
      padding: 10,
      alignItems: 'center'
  },

  buttonContainer:{
    width: 300,
    maxWidth: '80%',
    justifyContent: 'space-between',
    alignItems: 'center',
    shadowColor: 'black',
    shadowOffset: { width: 0, height: 2},
    shadowRadius: 4,
    shadowOpacity: 0.26,
    backgroundColor:'#fff',
    elevation: 8 ,
    padding: 20,
    borderRadius: 15,
    flexDirection: 'row',
    marginTop: 20
  }
});

export default GameScreen;
import React,{useState,useRef}来自“React”;
从“react native”导入{视图、文本、样式表、按钮、警报};
const generateRandomBetween=(最小、最大、排除)=>{
min=数学单元(min);
最大值=数学楼层(最大值);
const rndNum=Math.floor(Math.random()*(max-min))+min;
如果(rndNum==排除){
返回generateRandomBetween(最小、最大、排除);
}否则{
返回rndNum;
}
};
常量游戏屏幕=道具=>{
const[currentGuess,setCurrentGuess]=useState(generaterandomBevering(1,99,props.userChoice));
const currentLow=useRef(1);
const currentHigh=useRef(99);
const nextguesHandler=方向=>{
if((方向=='lower'&¤tGuessprops.userChoice)){
Alert.Alert('不要对我撒谎','你不能欺骗我'[{text:'对不起,我不会再撒谎!',style:'取消'}]);
}
}
如果(方向=='下'){
currentHigh.current=currentGuess;
}否则{
currentLow.current=currentGuess;
}
const nextNumber=发电机范围(currentLow.current、currentHigh.current、currentGuess);
当前猜测(下一个数字);
返回(
对手猜测
{currentGuess}
)
};
const styles=StyleSheet.create({
游戏屏幕容器:{
宽度:300,
maxWidth:'80%',
对齐项目:“居中”,
阴影颜色:“黑色”,
阴影偏移:{宽度:0,高度:2},
阴影半径:4,
阴影不透明度:0.26,
背景颜色:“#fff”,
立面图:8,
填充:20,
边界半径:15,
玛金托普:30
},
guessNumberContainer:{
边界宽度:2,
边框颜色:“FFC45D”,
填充:10,
边界半径:10,
玛吉:10,
对齐项目:“居中”,
辩护内容:'中心',
宽度:200
},
猜测文字:{
颜色:“FFC45D”,
尺寸:22
},
屏幕:{
弹性:1,
填充:10,
对齐项目:“中心”
},
按钮容器:{
宽度:300,
maxWidth:'80%',
justifyContent:'之间的空间',
对齐项目:“居中”,
阴影颜色:“黑色”,
阴影偏移:{宽度:0,高度:2},
阴影半径:4,
阴影不透明度:0.26,
背景颜色:“#fff”,
立面图:8,
填充:20,
边界半径:15,
flexDirection:'行',
玛金托普:20
}
});
导出默认游戏屏幕;

我不知道如何解决这个问题,甚至不知道从哪里开始。我已尝试将方向名称更改为其他名称,但仍然出现错误,我确保已导入React,我不知道我缺少了什么。

根据评论中的对话,我假设您想要这样的内容

const nextGuessHandler = direction => {
    if ((direction === 'lower' && currentGuess < props.userChoice) || (direction === 'greater' && currentGuess > props.userChoice) ) {
        Alert.alert('Don\'t lie to me', 'You Can\'t Trick Me' [{text: 'I won\'t lie again , SORRY!', style: 'cancel'}]);
    }

    if ( direction === 'lower') {
        currentHigh.current = currentGuess;
    } else {
        currentLow.current = currentGuess;
    }
}

const nextNumber...
const nextGuessHandler=direction=>{
if((方向=='lower'&¤tGuessprops.userChoice)){
Alert.Alert('不要对我撒谎','你不能欺骗我'[{text:'对不起,我不会再撒谎!',style:'取消'}]);
}
如果(方向=='下'){
currentHigh.current=currentGuess;
}否则{
currentLow.current=currentGuess;
}
}
康斯特下一个号码。。。

在这一行
if(direction==='lower'){
direction变量不存在。它还没有被定义。可能那块代码应该在
nextgueshandler
函数中。我对这方面有点初学者,所以我不知道这意味着什么。这是否意味着我必须把if(方向==‘下’)……在nextGuessHandler中?这取决于你想做什么。如果你没有编写代码,那么最好询问编写代码的人。但错误的原因是
方向
在声明之前被引用。好的,我如何在上面声明方向代码,我不是编写代码的人,而是来自一个youtube教程,但我只是尝试实践一下,效果不错!我真的很感激!这个错误现在已经消失了,但出现了一个新的错误,说“currentGuess不是函数(在'currentGuess(nextNumber'))这意味着什么?解决方法是什么。如果您也能帮我解决这个问题,我会非常感激。如果您试图将
currentGuess
的值更新为
nextNumber
您需要使用
setCurrentGuess(nextNumber)
。很抱歉打扰您这么多哈,现在是说找不到变量setcurrentGuess,我如何声明它检查您的字母大小写。它已经定义了…
const[currentGuess,setcurrentGuess]=useState(…
非常感谢!!它现在工作得很好