this.props.route.params.value 在初始屏幕上未定义。在反应原生

时间:2021-06-15 09:58:44

标签: javascript react-native parameters navigation

我通过反向导航发送参数。从屏幕 B 到 A. 但是当我控制台 this.props.route.params.value 然后我得到未定义的错误。请帮忙。我怎样才能得到我向后发送的数据。我不知道redux。有没有没有redux的快速方法。

这里是屏幕A

import { TouchableOpacity, View } from "react-native";

export default class patientReadings extends Component {
  constructor(props) {
    super(props);
    this.state = {};
  }
  render() {

console.log(this.props.route.params.value)
    return (
      <View>
        <TouchableOpacity
          onPress={() => this.props.navigation.navigate("B",{value1:"1"})}
        >
          button 1
        </TouchableOpacity>
        <TouchableOpacity
          onPress={() => this.props.navigation.navigate("B",{value2:"2"})}
        >
          button 2
        </TouchableOpacity>
        <TouchableOpacity
          onPress={() => this.props.navigation.navigate("B",{value3:"3"})}
        >
          button 3
        </TouchableOpacity>
      </View>
    );
  }
}

这里是屏幕 B

export default class medicalInput extends React.Component {
  constructor(props) {
    super(props);
    this.myRef = React.createRef();
    this.state = {};
  }

  validateInputs = (event) => {
    fetch("api", {
      method: "POST",
      headers: {
        Accept: "application/json, text/plain, */*",
        "Content-Type": "application/json",
      },

      body: JSON.stringify([
        {
          data,
        },
      ]),
    })
      .then((returnValue) => returnValue.json())
      .then((response) => {
        console.log(response);

        this.props.navigation.navigate("A", {value : this.props.route.params.value1+"1"});
      });
  };

  render() {
    return (
      <TouchableOpacity
        onPress={() => this.validateInputs()}
      ></TouchableOpacity>
    );
  }
}

0 个答案:

没有答案