Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

123
Views
Obtener múltiples elementos svg cuando los datos se cargan D3

Estoy tratando de hacer un svg rectangular dentro de un envoltorio svg div pero de alguna manera obtengo dos o, a veces, múltiples envoltorios svg div. Supongo que uno se crea cuando los datos no están disponibles y otro cuando el componente de actualización useEffect cuando se cargan los datos, pero podría estar equivocado. No sé por qué está sucediendo, puede haber un error tonto o un error lógico. Soy nuevo en el aprendizaje de D3, así que perdóname y guíame. Por favor ayuda. a continuación se muestra el código. Gracias

 import React, { useState, useEffect } from 'react'; import * as d3 from 'd3'; const LineChart = () => { const weatherDataUrl = 'https://gist.githubusercontent.com/anujshaanmydash/17687a309a87105ab8991a3dcfd8c7e3/raw/weatherData.json'; const [data, setData] = useState(null); useEffect(() => { d3.json(weatherDataUrl).then(setData); }, []); if (!data) { return <pre>Loading....</pre>; } console.log(data); const yAccessor = (d) => d.temperatureMax; const dateParser = d3.timeParse('%Y-%m-%d'); const xAccessor = (d) => dateParser(d.date); let dimensions = { width: 1200, height: 500, margins: { top: 15, right: 15, bottom: 30, left: 50, }, }; dimensions.boundedWidth = dimensions.width - dimensions.margins.left - dimensions.margins.right; dimensions.boundedHeight = dimensions.height - dimensions.margins.top - dimensions.margins.bottom; const wrapper = d3 .select('#Wrapper') .append('svg') .attr('width', dimensions.width) .attr('height', dimensions.height); const bounds = wrapper .append('g') .style( 'transform', `translate(${dimensions.margins.left}px,${dimensions.margins.top}px)` ); //Create Scales const yScale = d3 .scaleLinear() .domain(d3.extent(data, yAccessor)) .range([dimensions.boundedHeight, 0]); const freezingTempArea = yScale(32); const freezingTemp = bounds .append('rect') .attr('x', 0) .attr('width', dimensions.boundedWidth) .attr('y', freezingTempArea) .attr('height', dimensions.boundedHeight - freezingTempArea) .attr('fill', '#e0f3f3'); const xScale = d3 .scaleTime() .domain(d3.extent(data, xAccessor)) .range(0, dimensions.boundedWidth); return <div id='Wrapper'></div>; }; export default LineChart;
about 4 years ago · Santiago Gelvez
1 answers
Answer question

0

Lo más probable es que sea un problema de <StrictMode> (index.js). Algunas funciones como useEffect se invocan dos veces en el modo de desarrollo.

Puede ver que los datos se registran dos veces en la consola debido a esto.

¿Cómo evitar esto? @Dan Abramov

 useEffect(() => { let ignore = false; fetchStuff().then(res => { if (!ignore) setResult(res) }) return () => { ignore = true } }, [])

Lea más aquí - https://github.com/facebook/react/issues/24502

about 4 years ago · Santiago Gelvez Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!