繁体   English   中英

通过本机导航传递参数

[英]Pass params through react-native navigation

伙计们。

我现在遇到了一个问题,即将参数传递给单个项目屏幕。

问题是我正在尝试使用我传递的id来在fetch上使用它。

我检查了文档,它对我根本没有用。

这是代码:

onPress={ () => this.props.navigation.navigate("Item", { id: item.idItem })}

如您所见,我正在尝试传递id

现在,让我们进入下一个屏幕:

SingleItemScreen.js

async fetchItem() {
        this.setState({ isLoading: true });
        try {
            fetch(`https://www.items-example.com/api/json/v2/key/lookup.php?i=${id}`)
                .then((response) => response.json())
                .then((responseJson) => {
                    this.setState({
                        item: responseJson, 
                    });
                }).catch((error) => {
                    console.log(error);
                });
        } finally {
            this.setState({ isLoading: false });
        }
    }

我不知道如何传递id ,我是在组件中而not在函数中这样做。

我怎么可能通过它并在render()之上使用它?

您可以通过以下方式访问其他组件中的参数。

let {id} = this.props.route.params;

对 id 变量做任何你想做的事情。

暂无
暂无

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM