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

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.

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...
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.

Related

Is there a legit way to manipulate DOM from under React?

Given all efforts to make React work fast, I am still having performance issues when it comes to DOM manipulations governed by React. Is there a way to switch to direct DOM manipulations from under React without breaking it?
PS: I am specifically interested in removing DOM nodes.
A safe way to handle this is to build your performance-sensitive component entirely outside of React.
This can be done via web components. React has a page that explains how web components and React components can be used together over here.
Thus, you can have complete control over the shadow DOM within your web component to do whatever DOM manipulations you wish to do, and then you can insert your web compoment within React without any worry of React's virtual DOM algorithm messing with what you've done in the shadow DOM.
React is all about manipulating the state to trigger re-renders of the DOM. Instead of arbitrarily removing the element from the DOM as you would in jQuery, you should update the state by filtering out the item from props. items that will trigger a rerender of the DOM with the item removed.
For better manipulations you can use state for small and redux for midium to big projects.
useRef and useState are easy to use and manipulate.

React Implementation of Pannable and Zoomable element with Draggable Component inside it

I am trying to build a react application that can create a card component and connect them, and they also can pan and zoom the screen also drag those cards. Like the building window in Figma or Adobe XD. Can anyone suggest to me any popular and effective solution or packages for it. I have been doing some research for a few days now and I could not get a proper solution for it.
You can use drag&drop functionality, basically you need to handle callbacks when drag&drop events happen (onDragStart, onDragEnd, onDragEnter ...), you get data from the event parameter. When something is dropped, you update the state accordingly (add/remove item from list), and render them with map function. Some sample: https://codesandbox.io/s/reactdrag-and-drop-iq89y?file=/src/components/TaskList.jsx

React DIV dynamic layout algorithm to create a box-in-box view

I am trying to represent the following onto a web page. - that is a data model representation illustrating objects, attributes and child objects in a parent child hierarchy.
I was thinking to create a generic React component that would be able to store a single data object, but then also have that same object accept child instances in a sort of dynamic mechanism.
The result would be a nested-box type view, which would show all elements and nested children.
the child div, would need to have some sort of layout feature (much like the grid-layout features of popular UI frameworks (material-ui, Scemantic-ui, Zurb foundation)
at the end, The "model" would look something like this.
I dont even know where to start to build something like this. I am looking for some ideas to build a UI like this... the intention at a later stage would be to enable something like React-draggable to allow dragging of elements.
For infinite tree structure rendering purposes, you might need to look at recursion usage in React. I found some resources for you:
https://dev.to/baso53/recursive-rendering-in-react-building-a-universal-json-renderer-f59
https://medium.com/#suraj.rajan/recursion-using-reactjs-components-3c871f99fb2f
Basically, you need to create a function in the class which returns an instance of your class.

Generating Ember Components with D3

There is plenty of content online about using Ember.js with D3.js, particularly with making D3 visualizations as Ember Components. This is something I need to do for a project, but I'm much more familiar with D3 than I am with Ember.
Specifically, I'm using a D3 force layout whose nodes/vertices need to be interactive. More specifically, right-clicking a node needs to expose a context menu whose entries have the following requirements:
Each menu entry needs to link to a Route of our Ember App
Actions and appearance of the menu entries depends upon the data bound to the right-clicked node (and possibly the data bound to the nodes edges and neighbors)
I'm a bit overwhelmed by the number of choices in front of me, but here is my main question:
How can I generate SVG elements with D3 that link to Ember Routes?

How general is Sencha Touchs javascript?

Sencha Touch seems like an amazing way to develop mobile apps. I've seen posts by people incorporating Jquery, D3.
At the same time the posts describing customizing controls seems to be fairly narrow.
Adding the picture of a kitten next to the slider and labeling the slider seems kinda tame compared to what ios can do in terms of custom controls, at least in terms of examples available. Most blog posts imply you can extend the control objects in Sencha or the CSS file.
These posts are not quite what I'm looking for - that's my problem. I can't see any examples of anyone changing default controls in Sencha touch, but they make it sound as if it might be possible to do anything.
This is my question:
Is Sencha Touch able to build an iOS or Android App incorporating any javascript library or HTML5? Are there any limitations here?
To give an example I trying to implement a custom slider, where a touch along a continuous line or a circle like this color selector will enter new values. Further if you incorporate a library like protovis or D3 (or Raphael charts) can Sencha display anything the graph canvas element will otherwise display? Will it take touch input and interact with the graph libraries the way that the HTML5 graph does?
The post you mentioned is not about customizing controls, it's about displaying a list from bound store, instead of of using just Ext.XTemplate (the system with Ext.view.View) to generate HTML, it uses ComponentView to generate Ext.Components instead.
It's hard to tell what you're asking, what in particular are you trying to do?
To address some of the questions you added:
Charts in Sencha are implemented using Raphael, which uses SVG, therefore all the elements in the chart can be interacted with using HTML events.
Everything that Sencha generates is valid HTML, you can listen to HTML events, but components usually abstract the lower level events into something that is easier to consume, (for example a data view abstracts the click so that it passes the record being clicked along with the event).
Therefore, the answer to the question is, YES, Sencha can co-exist with regular HTML. If you want the full benefit of the framework, you should always create an Ext.Component so that your components can be easily used within the framework's layout containers.
It's very easy to misuse Ext when trying to write regular HTML and still place that within the layout rendering pipeline. Ext.Component has a built in way of creating HTML out of templates, see http://docs.sencha.com/touch/2-0/#!/api/Ext.Component-cfg-data and http://docs.sencha.com/touch/2-0/#!/api/Ext.Component-cfg-tpl

Categories

Resources