简体   繁体   English

带有自定义 React 组件返回对象 Object 的 D3 组织结构图

[英]D3 Org Chart with custom React components returning object Object

I'm creating an org chart using React and this library: https://github.com/bumbeishvili/org-chart .我正在使用 React 和这个库创建一个组织结构图: https : //github.com/bumbeishvili/org-chart It uses D3 to create the org chart.它使用 D3 创建组织结构图。

I want to use custom React components to style each node in the org chart, but when I try to set the node content to a React component, it returns {object Object} instead of actually rendering the component.我想使用自定义 React 组件来为组织结构图中的每个节点设置样式,但是当我尝试将节点内容设置为 React 组件时,它返回{object Object}而不是实际渲染组件。 在此处输入图片说明

Please take a look at this Stackblitz for reproducing the error: https://stackblitz.com/edit/d3-org-chart-react-integration-hooks-oysugz?file=OrgChart.js请查看此 Stackblitz 以重现错误: https ://stackblitz.com/edit/d3-org-chart-react-integration-hooks-oysugz?file=OrgChart.js

Here I try to set the node content to <TestOrgCard/> but as you can see, it does not render.在这里,我尝试将节点内容设置为<TestOrgCard/>但如您所见,它不会呈现。

Does anyone have an idea how to render cards using custom React components?有没有人知道如何使用自定义 React 组件渲染卡片?

You can't return a react component from the function you pass to .nodeContent() , it must be an HTML string.你不能从你传递给.nodeContent()的函数中返回一个 react 组件,它必须是一个 HTML 字符串。 Since you want to render an existing react component you can use ReactDOMServer.renderToStaticMarkup to get the equivalent HTML string.由于您想渲染现有的React组件,您可以使用ReactDOMServer.renderToStaticMarkup来获取等效的 HTML 字符串。

import ReactDOMServer from "react-dom/server";
...
const TestOrgCard = (props) => {
  return (
    <div>
      <h5> {props.name} </h5>
    </div>
  );
};
...
        .nodeContent((d, i, arr, state) => {
          return ReactDOMServer.renderToStaticMarkup(
            <TestOrgCard {...d.data} />
          );
        })

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM