繁体   English   中英

从 Render 中的循环获取变量到 ReactJS 中的 componentDidMount() 并将此变量放入查询参数中

[英]Get variable from loop in Render into componentDidMount() in ReactJS and put this variable in query parameter

我在渲染中有一个循环,它旋转坐标数组以在地图上定位标记。

{coords.map(({ lat, lng }, index) => (code here and etc))}

我想进入 componentDidMount 用循环索引 + 1 中的变量替换查询参数

在渲染之前,我有 componentDidMount 和来自数据库服务器的查询和响应。

我尝试将名称为“id” who = (index + 1) 的变量打包,但我不知道如何导入到我的查询中。

componentDidMount 的代码是:

async componentDidMount() {
    let id = (index + 1);

    const url = "http://192.168.0.1:8000/?date=2019-10-20&id=4&daysForward=8";
    const response = await fetch(url);
    let data = await response.json();
    this.setState({ dataAPI: data.aladinModel[0], loading: false });
    this.setState({ temp: data.aladinModel[0], loading: false });
    this.setState({ dats: data.aladinModel[0], loading: false });
  }

我想用 id=id 变量替换 id=4 ..

我怎样才能做到这一点 ?

你在寻找像字符串操作这样的东西吗? 例如:

 const myid = 4; const url = `http://192.168.0.1:8000/?date=2019-10-20&id=${myid}&daysForward=8`; console.log(url);

暂无
暂无

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

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