简体   繁体   English

undefined 不是 object (this.props.navigation.getParam)

[英]undefined is not an object (this.props.navigation.getParam)

FoodCreate.js FoodCreate.js

export class FoodCreate extends Component {
  state = {
    food: null,
    foodList: [],
  };

  submitFood = (food) => {
    this.setState({
      foodList: [
        ...this.state.foodList,
        {
          key: Math.random(),
          name: food,
        },
      ],
    });
    this.props.navigation.navigate("FoodList", {
      foodList: this.state.foodList,
      deleteFood: this.deleteFood,
    });
  };

  deleteFood = (key) => {
    this.setState({
      foodList: [...this.state.foodList.filter((item) => item.key != key)],
    });
  };

  render() {
    return (
      <Container>
        <Header>
          <Left>
            <Button transparent>
              <Icon
                name="arrow-back"
                onPress={() => this.props.navigation.goBack()}
                style={{ fontSize: 25, color: "red" }}
              />
            </Button>
          </Left>
          <Body>
            <Title>Add Food</Title>
          </Body>
          <Right>
            <Button transparent>
              <Icon
                name="checkmark"
                style={{ fontSize: 25, color: "red" }}
                onPress={() => {
                  this.submitFood(this.state.food);
                }}
              />
            </Button>
          </Right>
        </Header>
        <View style={{ alignItems: "center", top: hp("3%") }}>
          <TextInput
            placeholder="Food Name"
            placeholderTextColor="white"
            style={styles.inptFood}
            value={this.state.food}
            onChangeText={(food) => this.setState({ food })}
          />
        </View>
     </Container>
    );
  }
}

export default FoodCreate;

FoodList.js FoodList.js

export class FoodList extends Component {
  constructor(props) {
    super(props);
    this.state = {
      foodList: [],
    };
  }

  render() {
    return (
      <Button onPress={() => this.props.navigation.navigate("FoodCreate")}>
           Press to insert food
      </Button>
      <FlatList
        data={this.props.navigation.getParam("foodList")} <-------
        keyExtractor={(item, index) => item.key.toString()}
        renderItem={(data) => <ListItem itemDivider title={data.item.name} />}
      />
    );
  }
}
export default FoodList;

Hey everyone, I'm building a Diet App, the food gets created in FoodCreate.js by typing a food and pressing the checkmark Icon, and this will create the food and display it in the FoodList.js, keep it in mind that the first Screen to be displayed is FoodList.js.大家好,我正在构建一个饮食应用程序,通过键入食物并按下复选标记图标在 FoodCreate.js 中创建食物,这将创建食物并将其显示在 FoodList.js 中,请记住要显示的第一个屏幕是 FoodList.js。 When I run the code I get the following error: undefined is not an object (this.props.navigation.getParam)当我运行代码时,出现以下错误: undefined is not an object (this.props.navigation.getParam)

try尝试

   <FlatList
        data={props.route.params.foodList} <-------
        ...
        ...
      />

you must see document here: https://reactnavigation.org/docs/params/您必须在此处查看文档: https://reactnavigation.org/docs/params/

暂无
暂无

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

相关问题 TypeError: this.props.navigation.getParam is not a function 同时使用 ReactNative 传递参数 - TypeError: this.props.navigation.getParam is not a function while passing parameter using ReactNative TypeError:props.navigation.getParam 不是函数。 (在 &#39;props.navigation.getParam(&#39;docId&#39;)&#39; 中,&#39;props.navigation.getParam&#39; 未定义) - TypeError: props.navigation.getParam is not a function. (In 'props.navigation.getParam('docId')', 'props.navigation.getParam' is undefined) 未定义不是对象(评估_this2.props.navigation.navigate) - undefined is not an object (evaluating _this2.props.navigation.navigate) 未定义不是对象(评估this.props.navigation.navigate) - undefined is not an object (evaluation this.props.navigation.navigate) 未定义不是对象(评估this.props.navigation.navigate) - undefined is not an object (evaluating this.props.navigation.navigate) 未定义不是对象(评估&#39;_this.props.navigation.navigate&#39;) - undefined is not an object (evaluating '_this.props.navigation.navigate') 未定义不是对象(评估“ this.props.navigation”) - undefined is not an object (evaluating 'this.props.navigation') undefined 不是一个对象(评估&#39;_this3.props.navigation.navigate&#39;) - undefined is not an object (evaluating '_this3.props.navigation.navigate') undefined不是对象(评估this.props.navigation) - undefined is not an object (Evaluating this.props.navigation) 未定义不是对象(评估“ this.props.navigation”) - undefined is not an object (evaluating “this.props.navigation”)
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM