Axios无法通过ID获取对象反应

时间:2020-07-31 13:06:25

标签: javascript reactjs axios

我有一个Active.js组件,其中列出了axios认为的项目,然后有一个名称链接(子组件ActiveDetails.js),我希望该链接返回该特定对象用户单击以获取更多详细信息。但是它返回一个空对象。 ID未定义。 console
如何将对象的ID链接到特定的网址?

import numpy as np
 
np.add.reduceat(b,np.cumsum(np.concatenate([[0],a[:-1]])))
# array([11, 17, 15,  4])
import itertools as it

bi = iter(b)
[sum(it.islice(bi,x)) for x in a]
# [11, 17, 15, 4]

1 个答案:

答案 0 :(得分:1)

在您的VisitorDetails.js中,从道具中提取id,如下所示。当您使用react-router-dom传递ID时,id将被添加到params属性的match内,您可以从其中访问id值。

在这种情况下,与组件关联的路径为'/active/:id',则可以使用id属性访问值,如下所示

.........
.........
.........

componentDidMount() {
 const { id } = this.props.match.params
 axios.get(`http://localhost:8085/api/visitors/${id}`)
      .then(res => {
          this.setState({ visitors: res.data._embedded.visitorList });
      })
      .catch(error => {
          console.log(error)
      });

}
.........
.........
.........