Advertisement
Guest User

Untitled

a guest
Apr 9th, 2020
209
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1.     useEffect(
  2.         () => {
  3.             if (props.data && d3Container.current) {
  4.                 const svg = d3.select(d3Container.current);
  5.  
  6.                 // Bind D3 data
  7.                 const update = svg
  8.                     .append('g')
  9.                     .selectAll('text')
  10.                     .data(props.data);
  11.  
  12.                 // Enter new D3 elements
  13.                 update.enter()
  14.                     .append('text')
  15.                     .attr('x', (d, i) => i * 25)
  16.                     .attr('y', 40)
  17.                     .style('font-size', 24)
  18.                     .text((d: number) => d);
  19.  
  20.                 // Update existing D3 elements
  21.                 update
  22.                     .attr('x', (d, i) => i * 40)
  23.                     .text((d: number) => d);
  24.  
  25.                 // Remove old D3 elements
  26.                 update.exit()
  27.                     .remove();
  28.             }
  29.         },
  30.  
  31.         /*
  32.             useEffect has a dependency array (below). It's a list of dependency
  33.             variables for this useEffect block. The block will run after mount
  34.             and whenever any of these variables change. We still have to check
  35.             if the variables are valid, but we do not have to compare old props
  36.             to next props to decide whether to rerender.
  37.         */
  38.         [props.data, d3Container.current])
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement