无法访问通过React路由器传递的数据
我正在尝试将我从后端收到的数据传递到前端的另一页。我正在使用react-router-dom
和用
将我的数据从product.js
页面传递给result.js
前端上的页面。不过,我一直遇到此错误:
Uncaught TypeError: Cannot read properties of undefined (reading 'state')
并且代码总是在这条代码上破裂,它不会超越它(此代码存在于result.js
页面代码下方):(
<div className="score">hAPPi SCORE: <br/> <span>{this.props.location.state.data.data.score}</span></div>
product 。该代码的作用基本上是在用户的输入中(通过语音),将成绩单发送到后端,后端将发出反馈,分数等,然后这些数据将返回此> product.js
页面。从那里,我想将这些数据传递到result.js
页面。
class Product extends Component {
handleRedirect() {
// Redirect user to the results page
console.log(this.state.data)
console.log(this.state.data.data.feedback)
this.props.history.push("/results", {data: this.state.data}); // Sending backend data to frontend
}
}
export default withRouter(Product);
(result.js页面)这是应接收第一页的数据并在页面上显示数据的代码:
class ResultPage extends Component {
render() {
console.log(this.state.data) // trying to console log it, but it won't work
return (
<div className="Container">
<div className="result-section">
<div className="score">hAPPi SCORE: <br/> <span>{this.props.location.state.data.data.score}</span></div>
<div className="scale-container">
<Scale scale-id="scr-scale" style="score-scale" score={this.props.location.state.data.data.score}/>
</div>
</div>
<div className="analysis-section">
<div className="analysis-container">
<div className="analysis-title">
In-Depth Analysis
</div>
<div className="keywords-container">
<div className="section-titles">
Keywords
</div>
<div>
<ul>
{this.props.location.state.data.data.keywords.map(kw =>
<h3>{kw}</h3>
)}
</ul>
</div>
</div>
<div className="entity-container">
<div className="section-titles">
Entities
</div>
<div>
<ul>
{this.props.location.state.data.data.entities.map(en =>
<h3>{en}</h3>
)}
</ul>
</div>
</div>
<div>
<h1>Result</h1>
<div>
{this.props.location.state.data.data.feedback}
</div>
</div>
<hr></hr>
</div>
</div>
</div>
);
}
}
export default ResultPage;
每当我使用this.props.location.state.state时,我都会遇到错误。数据。(某物)
,但是当我用硬编码的数字/字符串替换时,该页面正常工作。我不确定我在将数据从product.js
页面传递到result.js
页面方面做错了什么,但是我很确定我在使用this.props.location ...
从结果访问数据时做错了什么。
一些帮助将不胜感激!
I'm trying to pass in data that I received from my backend to another page on my frontend. I'm using react-router-dom
and withRouter
to pass my data from the product.js
page to the result.js
page on the frontend. I keep getting this error though:
Uncaught TypeError: Cannot read properties of undefined (reading 'state')
And the code always breaks on this line of code, it doesn't go past it (this code is present in the result.js
page code down below):
<div className="score">hAPPi SCORE: <br/> <span>{this.props.location.state.data.data.score}</span></div>
(product.js PAGE) Here's the code of the piece in the react page that passes the received data from the backend to another page. What this code does is basically take in a user's input (through speech), send the transcript to the backend, and the backend will shoot out feedback, scores, etc..., and that data will then come back to this product.js
page. From there, I want to pass that data to the result.js
page.
class Product extends Component {
handleRedirect() {
// Redirect user to the results page
console.log(this.state.data)
console.log(this.state.data.data.feedback)
this.props.history.push("/results", {data: this.state.data}); // Sending backend data to frontend
}
}
export default withRouter(Product);
(result.js PAGE) Here's the code of the react page that is supposed to receive the data from the first page and display it on the page:
class ResultPage extends Component {
render() {
console.log(this.state.data) // trying to console log it, but it won't work
return (
<div className="Container">
<div className="result-section">
<div className="score">hAPPi SCORE: <br/> <span>{this.props.location.state.data.data.score}</span></div>
<div className="scale-container">
<Scale scale-id="scr-scale" style="score-scale" score={this.props.location.state.data.data.score}/>
</div>
</div>
<div className="analysis-section">
<div className="analysis-container">
<div className="analysis-title">
In-Depth Analysis
</div>
<div className="keywords-container">
<div className="section-titles">
Keywords
</div>
<div>
<ul>
{this.props.location.state.data.data.keywords.map(kw =>
<h3>{kw}</h3>
)}
</ul>
</div>
</div>
<div className="entity-container">
<div className="section-titles">
Entities
</div>
<div>
<ul>
{this.props.location.state.data.data.entities.map(en =>
<h3>{en}</h3>
)}
</ul>
</div>
</div>
<div>
<h1>Result</h1>
<div>
{this.props.location.state.data.data.feedback}
</div>
</div>
<hr></hr>
</div>
</div>
</div>
);
}
}
export default ResultPage;
I keep getting errors whenever I use the this.props.location.state.data.(something)
but when I replace that with hard-coded numbers/strings, the page works fine. I'm not sure I'm doing anything wrong in terms of passing the data from the product.js
page to the result.js
page, but I'm pretty sure I'm doing something wrong in accessing that data from the result.js page using this.props.location...
.
Some help would be greatly appreciated!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您尚未显示
resultpage
组件的呈现方式。它可以通过几种方式接收位置
对象作为道具。由
Route
组件直接渲染。示例:
用
使用
高阶组件。示例:
随着路由道具传递/注入,
resultpage
组件现在应该能够访问this.props.location.state.state?.data
值在路由状态传递的值产品页面。You haven't shown where/how the
ResultPage
component is rendered. It can receive thelocation
object as a prop in a couple ways.Rendered directly by a
Route
component on one of the rendering props.Example:
Decorated with the
withRouter
Higher Order Component.Example:
With the route props passed/injected, the
ResultPage
component should now be able to access athis.props.location.state?.data
value passed in route state from the product page.