Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/react-native/7.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
React native 我怎样才能用“数学随机”看到字符串?_React Native - Fatal编程技术网

React native 我怎样才能用“数学随机”看到字符串?

React native 我怎样才能用“数学随机”看到字符串?,react-native,React Native,我有一个字符串数组。例如const strings['hello'、'hi'、'good day']我试图随机选取一个字符串。我在控制台日志中看到日志91,但如何看到字符串 function Generate() { const randomString = Math.floor(Math.random() * (scenario.length)) return randomString } function MostLikely(this: any) { return (

我有一个字符串数组。例如
const strings['hello'、'hi'、'good day']
我试图随机选取一个字符串。我在控制台日志中看到
日志91
,但如何看到字符串

function Generate() {
    const randomString = Math.floor(Math.random() * (scenario.length))
    return randomString
}

function MostLikely(this: any) {
  return (
    <ImageBackground
      source={require('../../../assets/banner.jpg')}
      style={styles.image}>
      
  <Text>{}</Text>
      <Button title='Generate' onPress={() => console.log(Generate())} />
    </ImageBackground>
  );
}
函数生成(){
const randomString=Math.floor(Math.random()*(scenario.length))
返回随机字符串
}
功能最相似(此:任意){
返回(
{}
console.log(Generate())}/>
);
}
Generate()
返回一个随机数,您应使用该随机数为数组编制索引:

<Button title='Generate' onPress={() => console.log(scenario[Generate()])} />
console.log(场景[Generate()])}>

假设您使用的是功能组件,您可以使用并获得您想要的:

const scenarios = ['hello', 'hi', 'good day'];

function Generate() {
  const randomIndex = Math.floor(Math.random() * (scenarios.length));
  return scenarios[randomIndex];
}

function MostLikely() {
  const [randomString, setRandomString] = useState(Generate());

  const generateRandomString = useCallback(() => {
    setRandomString(Generate());
  }, []);


  return (
    <ImageBackground
      source={require('../../../assets/banner.jpg')}
      style={styles.image}
    >
      <Text>{randomString}</Text>
      <Button title="Generate" onPress={generateRandomString} />
    </ImageBackground>
  );
}
const scenarios=['hello','hi','good'];
函数生成(){
const randomIndex=Math.floor(Math.random()*(scenarios.length));
返回场景[随机索引];
}
函数MostLikely(){
const[randomString,setRandomString]=useState(Generate());
const generateRandomString=useCallback(()=>{
setRandomString(Generate());
}, []);
返回(
{randomString}
);
}

谢谢,我如何将它添加到那里{}?谢谢,这对理解它的工作原理有很大帮助。