在React Native中从导航接收参数

时间:2019-12-26 23:06:04

标签: javascript react-native react-navigation

我想通过导航发送参数。但是Im Im参数总是不确定的。 我不知道尝试什么,因为我真的不知道代码有什么问题。

在这里我发送参数。

 return (
    <View>
      {info.map((l, i) => (
        <ListItem
          key={i}
          title={l.NombreCliente}
          subtitle={
            <TouchableOpacity onPress={() => navigation.navigate("Abonos", { id: l.idClientes})} >
              <View
                style={{
                  flexDirection: "row",
                  paddingLeft: 10,
                  paddingTop: 5
                }}
              >
                <Text> Telefono: {l.Telefono} </Text>
                <Text> Cumpleaños: {l.FechaDeNacimiento} </Text>
              </View>
              <View
                style={{
                  flexDirection: "row",
                  paddingLeft: 10,
                  paddingTop: 5
                }}
              >
                <Text> Total Vendido: {l.total} </Text>
                <Text> Adeudo: {l.saldo_pendiente} </Text>
              </View>
            </TouchableOpacity>
          }
          bottomDivider
        />
      ))}
    </View>
  );

在这里,我接收并显示参数。

const id = navigation.getParam("id");
console.log(id);

return(
    <View>
        <Text>
            El id del cliente es: {id}
        </Text>
    </View>
);

2 个答案:

答案 0 :(得分:0)

我认为您必须从道具访问导航,并且应该是这样的:

const id = this.props.navigation.getParam("id");

答案 1 :(得分:0)

最常用于访问屏幕参数的this.props.navigation.state

const id = this.props.navigation.state.params.id

您可以调用getParam来获取具有回退功能的特定参数值,而不是直接访问该参数。

const id = this.props.navigation.getParam('id', '2');

如果idparamsundefined,请设置后备值2