Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

140
Vistas
React: Trying to set state via useEffect using local JSON data

I have a functional component that uses useState to set the mtvData state (via setMtvData).

However, I'm trying to call setMtvData in useEffect so that the mtvData state will have the json data and that json data will be passed to the Chart component.

But so far the Chart component just gets an empty string:

import Chart from './Chart.js';
import {useState, useEffect} from 'react';
import myjson from './data/myjson.json'

export default function App() {

  const [mtvData, setMtvData] = useState([]);

  useEffect(() => {
    setMtvData([...myjson]);
  },[])

  return (
     <Chart data={mtvData} />
  )

}
about 4 years ago · Juan Pablo Isaza
1 Respuestas
Responde la pregunta

0

Why are you setting the state on mount instead of initializing the state with your json?

import Chart from './Chart.js';
import {useState, useEffect} from 'react';
import myjson from './data/myjson.json'

export default function App() {

  const [mtvData, setMtvData] = useState([...myjson]);

  return (
     <Chart data={mtvData} />
  )

}
about 4 years ago · Juan Pablo Isaza Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda