React-native在获取时在循环中获取“需要循环”

Kir*_*tsu 5 reactjs react-native

我是反应原生的新手,目前正在尝试创建带有级别和练习的地图。现在,我只想获得每个级别的最大锻炼值,以创建与级别锻炼一样多的按钮。但我显示了此警告:“允许使用循环,但可能会导致未初始化的值。请考虑重构以消除对循环的需要。”

它显示了很长时间,但我没有 JSON。

这是我目前的代码:

  export async function getExercisesByLevel() {
  console.log("getExercicesByLevel");
  const url = 'http://XXX/Appli/appli-api.php?niveauExerciceCount'

  try {
    let req = await fetch(url)
    let response = await req.json()
    return response
  } catch(error) {
    console.log(error)
  }
}
Run Code Online (Sandbox Code Playgroud)

这是我用来检索 JSON 的代码。然后我只是调用该函数:

import React from 'react'
import { StyleSheet, View, Text } from 'react-native'
import { getLevels, getExercisesByLevel } from '../API/NiveauxAPI'

    class Niveaux extends React.Component {

      constructor(props) {
        super(props)
        this.state = { niveaux: "" }
      }

      componentDidMount() {
        {this._displayLevelsMap()}
      }

      _displayLevelsMap() {
        getExercisesByLevel().then(niveaux => {
          this.setState({
            niveaux: niveaux
          })
        })
      }

      render() {
        return (
          <View>
            <Text>Hello</Text>
          </View>
        )
      }
    }

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

    export default Niveaux
Run Code Online (Sandbox Code Playgroud)

所有这些都包含在 StackNavigator 中:

import { createStackNavigator, createAppContainer } from 'react-navigation'
import Niveaux from '../Components/Niveaux'

const SearchStackNavigator = createStackNavigator({
  Search: {
    screen: Niveaux,
    navigationOptions: {
      title: "Niveaux"
    }
  }
})

const App = createAppContainer(SearchStackNavigator)
export default App
Run Code Online (Sandbox Code Playgroud)

请让我知道我做错了什么(可能有很多事情:p)。

提前谢谢!