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

224
Views
Cómo pasar datos de la función asíncrona a useState() Flask API + React Frontend

Estoy tratando de obtener el estado de mis luces de tono usando mi API y, en función de esta información, represente la página (básicamente, si mi lámpara está encendida, quiero que mi interruptor de palanca en el sitio web ya esté activado una vez que se cargue la página) Actualmente estoy tratando de pasar datos obtenidos de mi API usando la función asíncrona a useState ()

Aquí está mi código

 const LightSwitch = (props) => { async function lightState(){ const response = await httpGet('http://192.168.1.124/get_light_state/id/' + props.id); let data = await response return data; } lightState().then(data => { console.log(data.Light); } ) const [toggleSwitch, setToggleSwitch] = useState();

Mi respuesta API es

 { "Light": false }

Aquí está mi código completo como referencia.

 import React, { useState } from "react"; import { Switch } from "antd"; import "./LightSwitch.css"; async function httpGet (url){ const response = await fetch(url,{method: 'GET'}); const string = await response.text(); const json = string === "" ? {} : JSON.parse(string); return json; } const LightSwitch = (props) => { async function lightState(){ const response = await httpGet('http://192.168.1.124/get_light_state/id/' + props.id); let data = await response return data; } lightState().then(data => { console.log(data.Light); } ) const [toggleSwitch, setToggleSwitch] = useState(); const onChangeSwitch = (checked) => { setToggleSwitch(!toggleSwitch); if (checked) { console.log(props.name + ' on'); httpGet('http://192.168.1.124/turn_on/id/' + props.id); }else{ console.log(props.name + ' off'); httpGet('http://192.168.1.124/turn_off/id/'+ props.id); } }; return ( <div> <Switch checked={toggleSwitch} onChange={onChangeSwitch}></Switch> </div> ); }; export default LightSwitch;
about 4 years ago · Juan Pablo Isaza
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!