Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/reactjs/24.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Reactjs 如何从promise返回数据,然后通过react组件中的道具传递_Reactjs_Expo_Es6 Promise - Fatal编程技术网

Reactjs 如何从promise返回数据,然后通过react组件中的道具传递

Reactjs 如何从promise返回数据,然后通过react组件中的道具传递,reactjs,expo,es6-promise,Reactjs,Expo,Es6 Promise,我正处于一场大斗争中。 我尝试从sendID以useState的形式传递数据,因为我需要它传递到我的组件展示的道具中,但它是未查找的 我不能退回它,因为它退回了一个承诺 import React, { useState, useEffect } from "react"; import DataService from "../services/Services"; import * as SecureStore from 'expo-secure-sto

我正处于一场大斗争中。 我尝试从sendID以useState的形式传递数据,因为我需要它传递到我的组件展示的道具中,但它是未查找的

我不能退回它,因为它退回了一个承诺

import React, { useState, useEffect } from "react";
import DataService from "../services/Services";
import * as SecureStore from 'expo-secure-store';
import Expositions from "./Expositions";


const Authenticate = () => {

  const[code, setCode] = useState();

  SecureStore.getItemAsync("device_id").then((response) => {
    console.log("stored id : " + response);
    sendId({"id" : response});
    }
  );
  
    function sendId(deviceId){
      console.log("object id send to api : " + JSON.stringify(deviceId));
      DataService.getInfo(JSON.stringify(deviceId))
        .then((response) => {
          SecureStore.setItemAsync("device_id", response.id);
          console.log("id from api : " + response.id);
          console.log(response.response);
          console.log("here");
          setCode({"id" : 3}); //does'nt work
        })
        .catch((e) => {
          console.log(e);
        });
    }

    //SecureStore.deleteItemAsync("device_id").then((reject) => {
    //});

    console.log("Data :" + JSON.stringify(code)); //undefined
    return (
      <Expositions infections={code}/> //undefined
    );
  
  };
  export default Authenticate;
import React,{useState,useffect}来自“React”;
从“./services/services”导入数据服务;
从“世博安全商店”导入*作为安全商店;
从“/Expositions”导入演示文稿;
const Authenticate=()=>{
const[code,setCode]=useState();
getItemAsync(“设备id”)。然后((响应)=>{
console.log(“存储id:+响应”);
sendId({“id”:response});
}
);
函数sendId(设备ID){
log(“对象id发送到api:+JSON.stringify(deviceId));
getInfo(JSON.stringify(deviceId))
。然后((响应)=>{
setItemAsync(“设备标识”,response.id);
log(“来自api的id:+response.id”);
console.log(response.response);
console.log(“此处”);
setCode({“id”:3});//不起作用
})
.catch((e)=>{
控制台日志(e);
});
}
//SecureStore.deleteItemAsync(“设备id”)。然后((拒绝)=>{
//});
console.log(“数据:+JSON.stringify(代码));//未定义
返回(
//未定义
);
};
导出默认验证;

在异步函数中,使用wait语句而不使用
。然后
用于诸如
SecureStore.getItemAsync
DataService.getInfo
之类的承诺。如果不使用wait,这些函数将在承诺执行完成之前返回值。

在异步函数中使用wait语句,而不使用
。然后
用于像
SecureStore.getItemAsync
DataService.getInfo
这样的承诺。如果不使用wait,这些函数将在承诺执行完成之前返回值。

sendId
getId
不是组件,也不是自定义挂钩,它们不能使用
useState
挂钩。修复该问题,然后定义一些有效的初始状态,并将实用程序函数放置在具有适当依赖关系的
useffect
hook中,这样就不会在每个渲染周期调用它们。您的
async
函数不
等待
任何东西,因此它们不需要声明为
async
。有条件地呈现
Expositions
或确保
Expositions
可以处理更新的道具。
sendId
getId
不是组件,也不是自定义挂钩,它们不能使用
useState
挂钩。修复该问题,然后定义一些有效的初始状态,并将实用程序函数放置在具有适当依赖关系的
useffect
hook中,这样就不会在每个渲染周期调用它们。您的
async
函数不
等待
任何东西,因此它们不需要声明为
async
。有条件地呈现
展示
或确保
展示
可以处理更新的道具。