d3'react.js单击d3树节点文本this.props.history.push给出了错误"“无法读取未定义的属性(Reading' props;)"
在D3和React.js的帮助下,我能够创建D3层次结构树
我想实现的目标是,单击每个节点文本(例如John,Luke,...)应该路由到每个单击文本的详细页面。
nodes
.append("text")
.attr("font-size", "17px")
.attr("text-anchor", "start")
.text(function (d) {
return `${d.data.name}`
})
.on("click", function (event, d) {
this.props.history.push(`/detail/${d.id}`);
});
构造函数
constructor(props) {
super(props);
console.log(this.props) // there is history in props
this.state = {
isClicked: ""
};
this.geGraph = this.getGraph.bind(this); // function where graph code presents
单击节点文本后的 。我遇到了错误。
Uncaught TypeError: Cannot read properties of undefined (reading 'history')
同样,当我在行 this.props.history.push
上进行调试时,
this 中没有 props
。
试图跟随此如何使用D3.JS图的React路由器将其重定向到另一个屏幕。仍然有同样的问题。当我尝试获取箭头功能时,
.on("click", (event, d) => this.props.history.push(`/detail/${d.id}`))
它给出了props的问题 und typeerror:无法读取未定义的属性(读取'props')
我在< route history = {createBrowserhistory()}/>
以及从“历史记录”;
import {create browserhistory}。并导出默认withRouter(mygraph)
从导入
import {fortrouter}从'react-router-dom';
。
i m使用路由器版本“ react-router-dom”:“^4.3.1”
和带有类型的基于类的组件。
如何解决此问题。
提前致谢!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您正在使用不同的范围上的范围,现在它的范围可以正常工作,它最好定义单击内部组件的功能并在单击事件上调用它。
或者,您可以将存储在另一个变量内部节点创建范围中并从中使用它。
Your are using this on different scope, its scope is functional now, its better to define a function for click inside component and call it on click event.
Or you can store this value in another variable inside nodes creation scope and use it from that.
根据JavaScript dom api 的引用此将是您将单击的DOM元素。
解决方案1
使用javascript
解决方案2
或使用
在构造函数中绑定点击事件处理程序
According to the javascript DOM API documentation, the reference to
this
will be the DOM element you will click on.Solution 1
use either Javascript arrow function:
Solution 2
or bind the click event handler in the constructor with
this