人。
我现在遇到问题,正在将参数传递到单个项目屏幕。
问题是我试图使用我要传递的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变量做任何您想做的事。
您可以通过以下方式访问其他组件中的参数。
let {id} = this.props.navigation.state.params;
使用id变量做任何您想做的事。