Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

The price of rendering elements using D3 in a React application?

I'm trying to understand how to integrate D3 and React. Specifically I'm trying to understand how using D3 to render visualizations impacts React. As explained in this excellent question and it's reply:

[...] there is currently no great way to work with React and D3 [...] this is because in the React world you don't do direct DOM manipulation, but in the d3 world that's the only thing you do.

The reply goes on to say

It seems to me that the current consensus for Force Layouts and the like is to simply drop out of React for those components and let d3 do its thing. This isn't ideal but it's way more performant.

What is the impact on React from letting D3 take care of rendering? Will it only impact the performance of the component using D3, or other components as well? Will direct manipulation of the DOM using D3 screw with React's virtual DOM in some way for example? I'm basically trying to get an idea of the price you have to pay for using D3.

like image 268
langkilde Avatar asked Jul 19 '17 20:07

langkilde


People also ask

Can you use D3 in React?

React hooks are one way to add an imperative escape hatch to allow D3. js to interact directly with the DOM. In the previous step, you used standard JSX to render an svg element as a starting point. From here, you can take advantage of the useRef and useEffect hook to link D3.

Can I use D3 With react native?

D3. js is the best, most powerful tool we have in React-Native (and maybe even all mobile development) for creating charts and data visualizations 📈.

What does D3 do JavaScript?

D3 is a JavaScript library and framework for creating visualizations. D3 creates visualizations by binding the data and graphical elements to the Document Object Model. D3 associates (binding) the data (stuff you want to visualize) with the DOM. This allows the user to manipulate, change or add to the DOM.


1 Answers

I've worked on a project (private, unfortunately) where I used D3 to represent a UML editor. Everything used SVG manipulation to draw an SVG representing the UML.

The Editor UI logic was implemented in a unique React element (UMLEditor), using TypeScript and D3. You could pass the editor properties to set changes in the UML, and callbacks to get back the data. For instance, you can drag and drop a UML class (in 60fps), but the UI only triggers two events (drag, and drop) to React callbacks.

The key is to have the logic and events separated from the UI manipulation, and have a small amount of big react elements, and not so many small elements.

It could manage a UML with around 4K classes in 30fps.

Edit: Let's define a small application. You have small react components with its children, like the root App element, a Navigation bar, a viewport, etc...

enter image description here

Every element but the UMLEditor have a small impact on the performance. UMLEditor element is a complex element without any React children. Every UI element inside it is rendered using D3. The real DOM of UMLEditor contains a complex SVG element managed entirely using D3.

To make this element interact with React, we pass as props callbacks for events like drag, drop, create new UML class... and one JavaScript class with all the D3 render logic.

We don't pass as prop the entire UML configuration, as it would have a negative impact on the performance. Instead, when we needed it for exporting purposes, the JavaScript class passed as a prop can give the whole UML configuration using a method.

like image 183
lilezek Avatar answered Oct 06 '22 07:10

lilezek