【问题标题】:Display SectionList from networks json file从网络 json 文件显示 SectionList
【发布时间】:2021-03-02 08:38:21
【问题描述】:

我想显示来自 raddit 网站的热门和有争议的网站列表。我能够使用注释掉的 FlatList 显示数据,但不能使用 SectionList。 我已经为数据设置了data.data,我个人认为设置数据是正确的。但它给出了一个错误。

export default function App() {
  const [isLoading, setLoading] = useState(true);
  const [data, setData] = useState({ title: "", data: [] });
  const width = Dimensions.get("window").width;

  const renderItem = ({ item }) => {
    console.log({ item });
    return (
      <View style={styles.item}>
        <Image style={styles.image} source={{ uri: item.data.thumbnail }} />
        <View style={{ flex: 1, flexDirection: "column" }}>
          <Text style={{ width: width - 50 }}>{item.data.title}</Text>
          <Text style={{ color: "#ababab", fontSize: 10 }}>
            {item.data.domain}
          </Text>
        </View>
      </View>
    );
  };
  const renderSectionHeader = ({ section }) => {
    return <Text>{section.title}</Text>;
  };
  const keyExtractor = (item, index) => item + index;

  useEffect(() => {
    const dataGet = async () => {
      try {
        const res1 = await fetch("https://www.reddit.com/r/newsokur/hot.json");
        const json1 = await res1.json();
        const res2 = await fetch(
          "https://www.reddit.com/r/newsokur/controversial.json"
        );
        const json2 = await res2.json();
        setData({ title: "popular", data: json1.data.children.slice(0, 5) });
        setData({ title: "controversial", data: json2.data.children.slice(0, 5) });
        setLoading(false);
      } catch (err) {
        console.log(err);
      }
    };
    dataGet();
  }, []);

  return (
    <View style={styles.container}>
      {isLoading ? (
        <ActivityIndicator
          style={{ flex: 1, alignItems: "center", justifyContent: "center" }}
          size="large"
          color="#0000ff"
        />
      ) : (
        <SectionList
          sections={data.data}
          keyExtractor={keyExtractor}
          renderItem={renderItem}
          renderSectionHeader={renderSectionHeader}
        />
        /* <FlatList
          data={data.data}
          renderItem={renderItem}
          keyExtractor={keyExtractor}
        /> */
      )}
    </View>
  );
}

以下是样式表的定义。

const styles = StyleSheet.create({
  container: {
    flex: 1,
    marginTop: StatusBar.currentHeight || 0,
    flexDirection: "column",
  },
  item: {
    flex: 1,
    flexDirection: "row",
    width: "100%",
  },
  image: {
    width: 50,
    height: 50,
  },
  text: {
    width: 50,
  },
});

【问题讨论】:

  • 你能告诉我们你发现的错误吗?
  • 由于这段代码没有输出任何错误,所以没有附加错误信息。那么,我很高兴地通知您,问题现在已经解决了!!

标签: javascript reactjs react-native react-native-sectionlist


【解决方案1】:

这个!

const [data, setData] = useState([{ title: "", data: [] }]);

这个!!

setData([
      { title: "popular", data: json1.data.children.slice(0, 5) },
      { title: "controversial", data: json2.data.children.slice(0, 5) },
    ]);

这个!!!

   <SectionList
      sections={data}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2021-10-01
    • 2017-08-17
    • 1970-01-01
    • 1970-01-01
    • 2020-12-18
    • 1970-01-01
    • 2017-08-13
    • 2012-01-21
    相关资源
    最近更新 更多