Undefined 不是一个评估 title.length React Native 的对象

步行者1

当我在 React Native 中提交表单时,出现以下错误 undefined is not an object(正在评估“title.length”)。当我在 Card.js 应该呈现表单中的数据时提交表单时会出现此问题。我已经检查过并且它可以很好地恢复数据,似乎是将其读取的数据呈现为未定义的问题。在这个错误之后,表单实际上提交成功并且 Card.js 显示数据。

在此处输入图片说明

卡.js

import React from "react";
import {
  StyleSheet,
  View,
  Text,
  ImageBackground,
  TouchableOpacity,
} from "react-native";

const Card = (props) => {
  const {
    navigation,
    title,
    address,
    homeType,
    description,
    image,
    yearBuilt,
    price,
    id,
  } = props;

  return (
    <TouchableOpacity
      onPress={() => props.navigation.navigate("HomeDetail", { houseId: id })}
    >
      <View style={styles.card}>
        <View style={styles.titleContainer}>
          <Text style={styles.title}>
            {title.length > 30 ? title.slice(0, 30) + "..." : title}
          </Text>
        </View>
        <View style={styles.imageContainer}>
          <ImageBackground source={{ uri: image }} style={styles.image}>
            <Text style={styles.price}>${price}</Text>
            <View style={styles.year}>
              <Text style={styles.yearText}>{yearBuilt}</Text>
            </View>
          </ImageBackground>
        </View>
        <View style={styles.description}>
          <Text style={styles.descriptionText}>
            {description.length > 100
              ? description.slice(0, 100) + "..."
              : description}
          </Text>
        </View>
      </View>
    </TouchableOpacity>
  );
};

const styles = StyleSheet.create({
  card: {
    shadowColor: "black",
    shadowOpacity: 0.25,
    shadowOffset: { width: 0, height: 2 },
    shadowRadius: 8,
    borderRadius: 10,
    backgroundColor: "#ffffff",
    elevation: 5,
    height: 300,
    margin: 10,
  },
  titleContainer: {
    height: "15%",
    padding: 10,
  },
  title: {
    fontSize: 18,
    fontWeight: "bold",
    color: "gray",
  },
  imageContainer: {
    width: "100%",
    height: "65%",
    overflow: "hidden",
  },
  image: {
    width: "100%",
    height: "100%",
    flexDirection: "row",
    justifyContent: "space-between",
    alignItems: "flex-end",
  },
  price: {
    fontSize: 30,
    color: "#fff",
    margin: 10,
  },
  year: {
    margin: 10,
    backgroundColor: "#2652B0",
    height: 25,
    width: 80,
    borderRadius: 5,
  },
  yearText: {
    fontSize: 20,
    color: "#fff",
    textAlign: "center",
  },
  description: {
    margin: 10,
  },
  descriptionText: {
    fontSize: 16,
    color: "gray",
  },
});

export default Card;

HomeListScreen.js

import React, { useEffect, useState } from "react";
import {
  StyleSheet,
  View,
  Text,
  FlatList,
  ActivityIndicator,
} from "react-native";
import { FloatingAction } from "react-native-floating-action";
import { useDispatch, useSelector } from "react-redux";

import Card from "../components/Card";
import * as houseAction from "../redux/actions/houseAction";

const HomeListScreen = (props) => {
  const dispatch = useDispatch();

  const [isLoading, setIsLoading] = useState(false);

  const { houses } = useSelector((state) => state.house);

  useEffect(() => {
    setIsLoading(true);
    dispatch(houseAction.fetchHouses())
      .then(() => setIsLoading(false))
      .catch(() => setIsLoading(false));
  }, [dispatch]);

  if (isLoading) {
    return (
      <View style={styles.centered}>
        <ActivityIndicator size="large" />
      </View>
    );
  }

  if (houses.length === 0 && !isLoading) {
    return (
      <View style={styles.centered}>
        <Text>No home found. You could add one!</Text>
      </View>
    );
  }

  return (
    <View style={styles.container}>
      <FlatList
        data={houses}
        keyExtractor={(item) => item._id}
        renderItem={({ item }) => (
          <Card
            navigation={props.navigation}
            title={item.title}
            address={item.address}
            homeType={item.homeType}
            description={item.description}
            price={item.price}
            image={item.image}
            yearBuilt={item.yearBuilt}
            id={item._id}
          />
        )}
      />
      <FloatingAction
        position="right"
        animated={false}
        showBackground={false}
        onPressMain={() => props.navigation.navigate("AddHome")}
      />
    </View>
  );
};

const styles = StyleSheet.create({
  container: {
    flex: 1,
  },
  centered: {
    flex: 1,
    justifyContent: "center",
    alignItems: "center",
  },
});

export default HomeListScreen;

19CS097 纳文库玛 M
      <Text style={styles.title}>
        { title ? (title.length > 30 ? title.slice(0, 30) + "..." : title):true}
      </Text>

确保标题不是未定义的。

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

undefined不是对象(评估“ allRowsIDs.length”)(React-Native)

React Native:'undefined 不是一个对象(评估'_react3.PropTypes.bool')

React Native:undefined 不是一个对象(评估'_this2.props.navigation.navigate')

React Native:TypeError:undefined不是一个对象(评估'_this.props.data.map')

TypeError:undefined不是一个对象(正在评估“ navigation.push”)React Native

TypeError:undefined不是React Native中的一个对象(评估'this.state.Gas')

TypeError:undefined不是React Native中的一个对象(评估'this.state.Gase')

React Native:TypeError:undefined不是一个对象(评估'this.props.navigation.navigate')

React Native undefined不是一个对象(评估“ Mapbox.GL.UserTrackingModes”)

React Native TypeError:undefined 不是一个对象(评估'_ref.item')

undefined 不是一个对象(评估 'Context._context') - React Native

React Native undefined不是一个对象(评估“ props.navigation.toggleDrawer”)

Undefined 不是一个对象(评估'this.props.navigation.navigate')React native

undefined 不是一个对象(评估'ToolbarAndroid.propTypes')react-native-general-calendars

错误 react-native-video : undefined 不是一个对象(评估 'RCTVideoInstance.Constants')

undefined 不是一个对象(评估 'this.onbuffer') React Native

React Native Render使用map渲染一个对象:undefined不是对象(评估“ currentLocation.coords”)

React-native .map undefined 不是一个对象

React Native,TypeError:undefined不是一个对象,当它是

React Native得到TypeError:undefined不是一个对象(正在评估'_this.state.apiData.items.map')

react-native:undefined中的抽屉导航错误不是一个对象(评估'_this.props.navigation.openDrawer()')

TypeError:undefined 不是 React Native 中的对象(正在评估“addTodo(title, description).then”)

React native, undefined 不是对象(评估....)

React Native, redux form, native base - onChange(event) 函数不起作用,undefined 不是一个评估 event.target.value 的对象

React Native:Undefined不是一个函数(评估this.props ...)

react-navigation:undefined不是一个对象(评估'Object.keys(routeConfigs)')

undefined 不是一个对象(评估 _react.default.PropTypes.func)

React Native:null不是一个对象(评估'_this.state.displayErrors')

React Native:另一个“未定义不是对象(评估 action.type)