G
G
gsdev992020-01-10 18:16:57
JavaScript
gsdev99, 2020-01-10 18:16:57

How to make Responsive svg (D3.js) correctly?

Hello everyone, tell me, please, how to make Responsive svg correctly?
Below is my implementation, but I think there is a better approach.
https://codepen.io/gsdev99/pen/QWwmeKp

Answer the question

In order to leave comments, you need to log in

1 answer(s)
P
profesor08, 2020-01-10
@profesor08

On resize you don't need throttle, remove it and it will work more clearly.
With such a thing, you will end badly if you suddenly have some kind of state in the component. And you have it. Because every time you change the state, you will add an additional handler resizeto window. Either delete the previous handlers somehow, or do it differently, for example, by adding a flag that will signal that the handler is already hanging, and you don’t need to add it again.

useEffect(() => {
    window.addEventListener('resize', onResize)
  }, [])

Personally, I would take this matter to a separate storeone mobx, where the width will be updated, and the component will be updated when necessary.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question