在React中使用Route参数作为数组索引

时间:2019-04-26 11:53:49

标签: javascript reactjs react-router

在我的Option Explicit Public Sub GetOptions() Dim html As Object, ws As Worksheet, headers() Dim i As Long, r As Long, c As Long, numRows As Long Set ws = ThisWorkbook.Worksheets("Sheet1") Set html = New HTMLDocument With CreateObject("MSXML2.XMLHTTP") .Open "GET", "https://www.europcar.co.za/", False .send html.body.innerHTML = .responseText Dim pickupBranches As Object, pickupBranchResults() Set pickupBranches = html.getElementById("PickupBranch_BranchID_id").getElementsByTagName("option") headers = Array("Pickup Location", "option value") numRows = pickupBranches.Length - 1 ReDim pickupBranchResults(1 To numRows, 1 To 2) For i = 1 To numRows pickupBranchResults(i, 1) = pickupBranches.item(i).innerText pickupBranchResults(i, 2) = pickupBranches.item(i).Value Next With ws .Cells(1, 1).Resize(1, UBound(headers) + 1) = headers .Cells(2, 1).Resize(UBound(pickupBranchResults, 1), UBound(pickupBranchResults, 2)) = pickupBranchResults End With End With End Sub 组件中,我像这样在组件安装中获取一些数据:

App

componentDidMount() { axios.get("https://jsonplaceholder.typicode.com/posts?_limit=10") .then(res => { this.setState({ posts: res.data }); }); } 函数中,我想根据页面ID仅通过特定帖子作为道具:

render

我想像这样从我的<BrowserRouter> <Route path="/about" component={About} /> <Route path="/posts/:postId" render={props => <Post {...props} post={this.state.posts[":postId"]} />} /> </BrowserRouter> 组件访问特定帖子:

Post

但是我无法使用componentDidMount() { this.setState({ id: this.props.match.params.postId, post: this.props.post }); } 访问帖子数据。显然,有问题的行是我尝试设置该发布数据的地方:

this.props.post

但是,我不知道如何将发布数据作为属性传递给render={props => <Post {...props} post={this.state.posts[":postId"]} />} 组件。我才刚刚开始学习React,所以将不胜感激。

0 个答案:

没有答案