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

133
Vistas
manipulating SWR data with local data in react

I have an API with SWR that returns data something like this:

const {data: data, error: error} = useSWR('fetchData', fetcher, { refreshInterval: 5000 })

I want to display the status of my data data.status which is easy:

<p>{data.status}</P>
<button onClick={changeStatus}>change status</button>

but there are sometimes that I need to manually change what needs to be displayed, for that I tried to store data.status to a variable first and then change it using two things:

if (data){
    status = data.status
}
const changeStatus = () => {
    status = 'bad'
}

which simply doesn't change status AND

const [status, setStatus] = useState('');
if (data){
    setStatus(data.status)
}
const changeStatus = () => {
    setStatus('bad')
}

which gives me "too many iterations" error!

So how can I manually change the data I get from SWR in a way that it still calls the api every 5 seconds and update the data accordingly?

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

0

you cannot set it like that. you need to use useEffect if you want to store status in a state

useEffect(() => {
  if (data) {
    setStatus(data.status);
  }
}, [data])
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