【问题标题】:I'm unable to render items on screen我无法在屏幕上呈现项目
【发布时间】:2021-08-05 06:12:23
【问题描述】:

我决定我需要在我的应用程序中使用 redux,并开始更改我的应用程序以使用 redux。我很难将数据列表呈现到我的屏幕上,控制台中没有显示错误。我已经设置了商店和减速器,并且正在使用模型,一切似乎都运行良好。我的屏幕正在渲染,但我的商店中的数据没有显示。谁能看出我哪里出错了?

import * as React from "react";
import {
  View,
  StyleSheet,
  FlatList,
  SafeAreaView,
  Dimensions,
  TouchableOpacity,
  Image,
  Text,
} from "react-native";
import { useSelector } from "react-redux";

import BigButton from "../components/BigButton.js";
import HomeScreenImage from "../components/HomeScreenImage.js";
//import HomeData from "../data/HomeData";
import colors from "../constants/colors.js";

const HomeScreen = (props, navigation) => {
  const exercise = useSelector((state) => state.exercise.homeData);
  return (
    <View style={styles.containerTop}>
      <View>
        <HomeScreenImage style={styles.top} />
        <View style={styles.top}>
          <BigButton title="Beast" />
        </View>
      </View>
      <SafeAreaView style={styles.flatListContainer}>
        <FlatList
          data={exercise}
          keyExtractor={(item) => item.id}
          renderItem={(itemData) => (
            <TouchableOpacity
              activeOpacity={0.8}
              style={styles.container}
              onPress={() => {
                {
                  if (itemData.item.id == 1) {
                    props.navigation.push("Body");
                  } else if (itemData.item.id == 2) {
                    props.navigation.push("PreMade");
                  } else if (itemData.item.id == 3) {
                    props.navigation.push("Stats");
                  } else if (itemData.item.id == 4) {
                    props.navigation.push("History");
                  } else if (itemData.item.id == 5) {
                    props.navigation.push("CreateTrack");
                  } else props.navigation.push("TrackList");
                }
              }}
            >
              <View style={styles.cardContainer}>
                <Image style={styles.imageStyle} source={itemData.Item.image} />
                <View style={styles.infoStyle}>
                  <Text style={styles.titleStyle}>{itemData.item.name}</Text>
                  <Text style={styles.bodyTextStyle}>{itemData.item.body}</Text>
                </View>
              </View>
            </TouchableOpacity>
          )}
        />
      </SafeAreaView>
    </View>
  );
};

下面的减速器

import HOMEDATA from "../../data/HomeData";
import CATEGORIES from "../../data/CategoryData";
import EXERCISE from "../../data/ExerciseData";
import MEMBERSHIPDATA from "../../data/MembershipData";

const initialState = {
  homeData: HOMEDATA,
  categories: CATEGORIES,
  exercises: EXERCISE,
  membershipData: MEMBERSHIPDATA,
};

export default (state = initialState, action) => {
  return state;
};

【问题讨论】:

  • 假设 useSelector 如果返回一个数组,我看不出有什么问题。尝试将keyExtractor 更改为keyExtractor={(item) =&gt; item.id.toString()}(但我怀疑它会解决问题)。您是否尝试过使用 exercise 的常量值而不是从商店“获取”?
  • 感谢您的回复。 Iv 在 App.js 中运行 console.log(store.getState())。在控制台中,我得到了回复:对象未定义。我将编辑我的问题以包含减速器,看看它是否能让人们看到更清晰。

标签: react-native redux react-redux


【解决方案1】:

我找到了解决方案。导出数据的方式不适用于 react-redux。以前很好,但是使用导出时,使用redux它不会导入它,让我在屏幕上渲染它。

这就是我之前的样子:

import Category from "../models/Category";

export const CATEGORIES = [
  new Category(
    "c1",
    "Chest",
    require("../assets/images/FlatBenchPress.jpg"),
    "Time To Unleash The Beast"
  ),
  new Category(
    "c3",
    "Shoulders",
    require("../assets/images/ShouldersPic.jpg"),
    "5% Human, 95% Beast, 100% Prepared To Destroy My Challenges"
  ),
  new Category(
    "c2",
    "Back",
    require("../assets/images/ChickB.jpg"),
    "The Pain You Feel Today, Will Be The Strenght You Need Tomorrow"
  ),

  new Category(
    "c4",
    "Biceps",
    require("../assets/images/Bicep.jpg"),
    "You Gotta Be A Beast There Is No Other Option"
  ),
  new Category(
    "c5",
    "Triceps",
    require("../assets/images/TriPic.jpg"),
    "Train Strong, Positive And Full Of Beast Mode"
  ),
  new Category(
    "c8",
    "Forearms",
    require("../assets/images/MenuTop.jpg"),
    "A Beast Never Fails"
  ),
  new Category(
    "c6",
    "Abs",
    require("../assets/images/AbsPic.jpg"),
    "There Is Time To Rest But It Is Not That Time Now, Now Is Time For Beast Mode"
  ),
  new Category(
    "c7",
    "Legs",
    require("../assets/images/LegsPic.jpg"),
    "A Real Beast Is Put To The Test On Legs Day, The Beast Never Fails"
  ),

  new Category(
    "c9",
    "Cardio",
    require("../assets/images/CardioPic.jpg"),
    "Sweat Out The Weakness It Is A Poison That Must Be Purged"
  ),
];

这就是解决方案:

import Category from "../models/Category";

const CATEGORIES = [
  new Category(
    "c1",
    "Chest",
    require("../assets/images/FlatBenchPress.jpg"),
    "Time To Unleash The Beast"
  ),
  new Category(
    "c3",
    "Shoulders",
    require("../assets/images/ShouldersPic.jpg"),
    "5% Human, 95% Beast, 100% Prepared To Destroy My Challenges"
  ),
  new Category(
    "c2",
    "Back",
    require("../assets/images/ChickB.jpg"),
    "The Pain You Feel Today, Will Be The Strenght You Need Tomorrow"
  ),

  new Category(
    "c4",
    "Biceps",
    require("../assets/images/Bicep.jpg"),
    "You Gotta Be A Beast There Is No Other Option"
  ),
  new Category(
    "c5",
    "Triceps",
    require("../assets/images/TriPic.jpg"),
    "Train Strong, Positive And Full Of Beast Mode"
  ),
  new Category(
    "c8",
    "Forearms",
    require("../assets/images/MenuTop.jpg"),
    "A Beast Never Fails"
  ),
  new Category(
    "c6",
    "Abs",
    require("../assets/images/AbsPic.jpg"),
    "There Is Time To Rest But It Is Not That Time Now, Now Is Time For Beast Mode"
  ),
  new Category(
    "c7",
    "Legs",
    require("../assets/images/LegsPic.jpg"),
    "A Real Beast Is Put To The Test On Legs Day, The Beast Never Fails"
  ),

  new Category(
    "c9",
    "Cardio",
    require("../assets/images/CardioPic.jpg"),
    "Sweat Out The Weakness It Is A Poison That Must Be Purged"
  ),
];

export default CATEGORIES;

我不知道为什么这有效,但确实有效。如果有人知道它为什么起作用,请在此处发布以供大家参考。

【讨论】:

    猜你喜欢
    • 2022-01-16
    • 2022-08-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-09-16
    • 1970-01-01
    • 1970-01-01
    • 2019-11-03
    相关资源
    最近更新 更多