简体   繁体   English

我应该在平面列表中输入什么来获取 json API,我得到了响应,我可以 consol.log 它

[英]What should I type in flat list to fetch that json API, I got the response and I can consol.log it

I try to fetch data from Firebase with Axios and Redux to handle the state, Everything Is fine and I could console.log the data too, but FlatList doesn't show any data, Here is my Code: I try to fetch data from Firebase with Axios and Redux to handle the state, Everything Is fine and I could console.log the data too, but FlatList doesn't show any data, Here is my Code:

Here is the Action of Redux.这是 Redux 的动作。

import axios from "axios";
import {  GET_PRODUCTS } from "./types";

export const fetchData = () => {
  return (dispatch) => {
    return axios
      .get("https://learn-efc13-default-rtdb.firebaseio.com/products.json")
      .then((response) => {
        return response.data;
      })
      .then((data) => {
        dispatch({
          type: GET_PRODUCTS,
          payload: data,
        });
        console.log(data);
      })

      .catch((error) => {
        throw error;
      });
  };
};

Here is the reducer这是减速机

import { GET_PRODUCTS } from "./types";

const initState = {
  availableProducts: [],
};

const reducer = (state = initState, action) => {
  switch (action.type) {
    case GET_PRODUCTS:
      return {
        availableProducts: action.availableProducts,
      };
    default:
      return state;
  }
};
export default reducer;

And finally, the Screen, That contains the FlatList最后是包含 FlatList 的屏幕

import React, { useEffect } from "react";
import { View, Text, FlatList } from "react-native";

import { useDispatch, useSelector } from "react-redux";
import { fetchData } from "./store/action";

function ScreenMain() {
  useEffect(() => {
    dispatch(fetchData());
  }, [dispatch]);

  const data = useSelector((state) => state.availableProducts);
  const dispatch = useDispatch();

  return (
    <View style={styles.con}>
      <FlatList
        data={data}
        keyExtractor={(item) => item.id.toString()}
        renderItem={({ item }) => (
          <Text>
            {item.name}
            {item.age}
          </Text>
        )}
      />
    </View>
  );
}

export default ScreenMain;

I Added the useEffect hook to console.log data我在 console.log 数据中添加了 useEffect 挂钩

can you wrap the <FlatList /> like this:你能像这样包装<FlatList />

{data && 
  <FlatList
     data={data}
     keyExtractor={(item) => item.id.toString()}
     renderItem={({ item }) => (
       <Text>
         {item.name}
         {item.age}
       </Text>
     )}
 />}

暂无
暂无

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

相关问题 如何获取 Firebase Json api,我得到了响应,我可以解决它,但是 FlatList 的问题 - How to fetch Firebase Json api, I got response and i can consol log it, But the problem with FlatList 如何在平面列表中调用另一个 api? - How can I call another api in flat list? 我如何解析提取调用中的简单json响应 - How can i parse simple json response in fetch call I'm trying to fetch my json site to enable API, but but got an error “Unexpected token &lt; in JSON at position 0” - I'm trying to fetch my json site to enable API, but but got an error “Unexpected token < in JSON at position 0” 我如何从获取API React Native获取响应 - How can i get response from fetch API React Native 如何获取 API? - How can I fetch API? 我有一个 json 数据,我想知道如何使用 fetch api 访问这些值? - I have a json data and I wanted to know how I can access the values using fetch api? 我可以在我的 console.log 中显示 API 数据,但我不能用它来设置状态。 . . 我该如何设置状态? (获取,反应本机) - I could display API data in my console.log, but I can't setState with it . . . how can I setState? (fetch, React Native) 如何有条件地更改 React Native 中平面列表上的数据? - How can I conditionally change data on a flat list in React Native? 如何在React Native上使用访存功能登录API并捕获登录错误? - How can I log in to an API and catch login errors using fetch on React Native?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM