这是一个演示API,它试图将其值输出到平面列表中

这就是代码

const Search =() =>{
const[listing, setListing] = useState([]);
  const[isLoading, setIsLoading]= useState(true);
  const[error,setError] = useState(null);

   useEffect(() =>{
    getProduct();
   }, []);

  const getProduct = () =>{
    const url = 'https://www.alphavantage.co/query?function=TIME_SERIES_INTRADAY&symbol=IBM&interval=5min&apikey=demo'
      fetch(url)
      .then((response) => {
        if(!response.ok){
          throw new Error('something is wrong')
        }
          return response.json();
      }).then((data) =>{
        setListing(data);
        setIsLoading(false)
        console.log(data);
      }).catch((error) =>{
        setError(error.message);
        setIsLoading(false);
        console.log(error)
      })
  }
     return (
      <View>
        {isLoading ? (
          <ActivityIndicator color={'blue'} size='large'/>
        ): error ? <Text>{error}</Text>: (
          <FlatList
            data={listing}
            renderItem={({item}) => {
              <View>
                <Text>{item.Data}</Text>
              </View>
            }}
          />
        )}
      </View>
     )
       
}

以下是演示输出:

"1.信息":"盘中(5分钟)开、高、低、收价量", "2.Symbol":"IBM", "3.上次刷新时间":"2023-11-24 17:00:00", "4.Interval":"5分钟", "5.输出大小":"紧凑型", "6.时区":"美国/东部" }, 《时间序列(5分钟)》:{ 《2023-11-24 17:00:00》:{ "1.打开":"155.1800", "2.高":"155.1800", "3.低":"155.1800", "4.Close":"155.1800", "5.音量":"335049" }, 请帮帮我,我是react 本地化的初学者

推荐答案

因此,您发布的代码示例存在多个问题. 让我们先从基础开始. Flatlist使用数组,API返回Object.你需要把它转换成array.

要获得您在演示输出中发布的所需数据,您需要执行如下转换:

Object.values(data['Meta Data'])

您需要使用此数据作为参数调用setListing.

接下来,您的扁平列表中的renderItem函数也是错误的.RenderItem需要返回JSX的函数.仔细查看代码,renderItem不会返回任何内容.您应该删除花括号,如下所示:

renderItem={({item, index}) => 
              <View>
                <Text>{item}</Text>
              </View>
            }

或显式使用RETURN关键字.

完成这些更改后,您的代码应该可以按预期工作.

请找到工作代码:

const[listing, setListing] = useState([]);
  const[isLoading, setIsLoading]= useState(true);
  const[error,setError] = useState(null);

   useEffect(() =>{
    getProduct();
   }, []);

  const getProduct = () =>{
    const url = 'https://www.alphavantage.co/query?function=TIME_SERIES_INTRADAY&symbol=IBM&interval=5min&apikey=demo'
      fetch(url)
      .then((response) => {
        console.log('calling data')
        if(!response.ok){
          throw new Error('something is wrong')
        }
          return response.json();
      }).then((data) =>{
        setListing(Object.values(data['Meta Data']));
        setIsLoading(false)
        console.log( Object.values(data['Meta Data']));
      }).catch((error) =>{
        setError(error.message);
        setIsLoading(false);
        console.log(error)
      })
  }

     return (
      <View>
        {isLoading ? (
          <ActivityIndicator color={'blue'} size='large'/>
        ): error ? <Text>{error}</Text>: (
      <View>
        
     
          <FlatList
            data={listing}
            renderItem={({item, index}) => 
              <View>
                <Text>{item}</Text>
              </View>
            }
          />
           </View>
        )}
      </View>
     )
} 

React-native相关问答推荐

错误:HostBody::get for prop NativeUnimoduleProxy中出现异常- Android虚拟设备(AVD)使用Expo测试React Native App时崩溃

复制__自持props

React Native Expo - 不再支持 Node.js 版本 11.13.0

React.js - 为所有组件方法使用属性初始化器

更改整个 React Native App 的语言

redux-saga:如何以编程方式为yields 创建多个calls/side-effects?

fetch API 总是返回 {"_U": 0, "_V": 0, "_W": null, "_X": null}

命令失败:gradlew.bat installDebug error whenever installing dependencies like navigation, firebase, icons etc in React-Native

FlatList contentContainerStyle -> justifyContent: 'center' 导致滚动问题

Jest 的配置在更新到 26.x 后抛出type ErrorHandler = (error: mixed, isFatal: boolean) => void

使用 applicationIdSuffix 时 React Native 应用程序不会启动

使用 Jest 测试 React Native 应用程序

ScrollView bounce的 2 种不同背景 colored颜色

React Native 检测点击 View

Android 依赖 'com.google.android.gms:play-services-stats' 的编译(16.0.1)和运行时(17.0.0)类路径有不同的版本

是否需要使用 StyleSheet.create?

React Native Navigation 组件路由问题

在 webview 中检测touch 是 Apple Pencil 还是手指(react-native)

当前文件夹中的 react-native init

使用 React Native 一次启动多个 Animated.timing