d3'react.js单击d3树节点文本this.props.history.push给出了错误"“无法读取未定义的属性(Reading' props;)"

发布于 2025-01-24 18:47:27 字数 1859 浏览 2 评论 0 原文

在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” 和带有类型的基于类的组件。

如何解决此问题。

提前致谢!

With the help of d3 and react.js, I am able to create d3 hierarchy tree

enter image description here

What I want to achieve is, On click of each node text (eg John, Luke, ...) it should route to detail page of each clicked text.

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

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

after clicking the node text. I getting error.

Uncaught TypeError: Cannot read properties of undefined (reading 'history')

Also when I debug on line this.props.history.push, there is no props in this .

Tried to follow this How to use React Router with d3.js Graphs to redirect to another screen . still got the same issue. and when i try to go for arrow function

.on("click", (event, d) => this.props.history.push(`/detail/${d.id}`))

it gives issue for props this time Uncaught TypeError: Cannot read properties of undefined (reading 'props')

I have added history in <Route history={createBrowserHistory()}/> as well import { createBrowserHistory } from "history";. and did export default withRouter(MyGraph) imported from import { withRouter } from 'react-router-dom';.

I m using router version "react-router-dom": "^4.3.1" and class based components with typescript.

How do I resolve this issue.

Thanks in advance!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

愿与i 2025-01-31 18:47:27

您正在使用不同的范围上的范围,现在它的范围可以正常工作,它最好定义单击内部组件的功能并在单击事件上调用它。

或者,您可以将存储在另一个变量内部节点创建范围中并从中使用它。

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.

孤独患者 2025-01-31 18:47:27

根据JavaScript dom api 的引用此将是您将单击的DOM元素。

nodes
    .append("text")
    .attr("font-size", "17px")
    .attr("text-anchor", "start")
    .text(function (d) {
        return `${d.data.name}`
    })
    .on("click", function (event, d) {
        // try console.log here
        console.log(this);   // <--- most likely be a DOM element you clicked
        this.props.history.push(`/detail/${d.id}`); // <--- therefore this.props does not exist
});

解决方案1

使用javascript

.on("click", (event, d) => {
        console.log(this);   // <--- this should refer to the parent object
        this.props.history.push(`/detail/${d.id}`);
});

解决方案2

或使用 在构造函数中绑定点击事件处理程序

constructor(props) {
    super(props);
    console.log(this.props)                  
    this.state = {
      isClicked: ""
      };
    this.geGraph = this.getGraph.bind(this);
    this.nodeClickHandler = this.nodeClickHandler.bind(this);
}

function nodeClickHandler (event, d) {
    this.props.history.push(`/detail/${d.id}`);
}
nodes
    .append("text")
    .attr("font-size", "17px")
    .attr("text-anchor", "start")
    .text(function (d) {
        return `${d.data.name}`
    })
    .on("click", this.nodeClickHandler);

According to the javascript DOM API documentation, the reference to this will be the DOM element you will click on.

nodes
    .append("text")
    .attr("font-size", "17px")
    .attr("text-anchor", "start")
    .text(function (d) {
        return `${d.data.name}`
    })
    .on("click", function (event, d) {
        // try console.log here
        console.log(this);   // <--- most likely be a DOM element you clicked
        this.props.history.push(`/detail/${d.id}`); // <--- therefore this.props does not exist
});

Solution 1

use either Javascript arrow function:

.on("click", (event, d) => {
        console.log(this);   // <--- this should refer to the parent object
        this.props.history.push(`/detail/${d.id}`);
});

Solution 2

or bind the click event handler in the constructor with this

constructor(props) {
    super(props);
    console.log(this.props)                  
    this.state = {
      isClicked: ""
      };
    this.geGraph = this.getGraph.bind(this);
    this.nodeClickHandler = this.nodeClickHandler.bind(this);
}

function nodeClickHandler (event, d) {
    this.props.history.push(`/detail/${d.id}`);
}
nodes
    .append("text")
    .attr("font-size", "17px")
    .attr("text-anchor", "start")
    .text(function (d) {
        return `${d.data.name}`
    })
    .on("click", this.nodeClickHandler);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文