Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/swift/17.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 React hook未使用异步等待设置对象状态。始终未定义_Reactjs_Async Await_React Hooks_Es6 Promise_React Hook Form - Fatal编程技术网

Reactjs React hook未使用异步等待设置对象状态。始终未定义

Reactjs React hook未使用异步等待设置对象状态。始终未定义,reactjs,async-await,react-hooks,es6-promise,react-hook-form,Reactjs,Async Await,React Hooks,Es6 Promise,React Hook Form,为名为UpdateSOR的组件编写了一个UpdateSOR函数,该组件可以从上一个名为SOR的页面进行导航 UpdateSOR是一个表单,应该从api中预先填写。因此,如果用户直接访问此页面或上一页面,我会在useffect中进行下面编写的api调用,并设置const[SOR]的值,该值将在表单中使用,以便在页面加载后立即预填充 export default function UpdateSOR(props) { const [SOR, setResult] = React.useSt

为名为UpdateSOR的组件编写了一个UpdateSOR函数,该组件可以从上一个名为SOR的页面进行导航

UpdateSOR是一个表单,应该从api中预先填写。因此,如果用户直接访问此页面或上一页面,我会在useffect中进行下面编写的api调用,并设置const[SOR]的值,该值将在表单中使用,以便在页面加载后立即预填充

 export default function UpdateSOR(props) {
    const [SOR, setResult] = React.useState({}); // empty object
    useEffect(() => {
        const getSOR = async () => {
            console.log("Inside async");

            await QueryService.getSOR(props.match.params.sorID).then(
              (result) => {
                console.log("Inside await then");
                if (result.status === 200) {
                  setResult(result.data.data.dataItems); // set the value for SOR here 
                  console.log("result.data.data.dataItems" ,result.data.data.dataItems);
                }
              }
            ).catch(error => {
              console.error(error);
            });
            getSOR();

          }
      }, []);


console.log("Value of SOR" , SOR) // value of sor is empty object which was during initialization and SOR is not coming from the api written above in useEffect :(

 return (

    <Container fluid={true} className="pt-4 pb-4 pl-0 pr-0">
        <Row className={`align-items-center ${classes.header}`}>
            <Col lg="10">
                <h4 className="pl-4">Form SOR</h4>
            </Col>
            <Col lg="1">
                <Button type="submit" variant="contained" color="primary" onClick={handleSave} className="mr-3 float-right">
                    Save
                    </Button>
            </Col>
            <Col lg="1">
                <Button variant="contained" color="primary" onClick={handleCancel} className="mr-3 float-right">
                    Cancel
                    </Button>
            </Col>
        </Row>
        <form className={`${classes.form} ${classes.body}`} noValidate autoComplete="off">
            <Row>
                <Col lg="2" onChange = {(event) => setSelectedSorName(event.target.value)}>
                    <CommonInput
                        id="sor_name"
                        label="SOR Name"
                        defaultValue={SOR.SOR_NAME}
                        disabled={false}
                        multiline={false}
                        rows="1" />
                </Col>
            </Row>
)

最后一行是什么?常量[SOR]…?请添加带有React类或功能组件的完整示例。我不清楚您在哪里使用上面的代码。您是否尝试调试您的逻辑?如果
SOR
依赖于
setResult
设置的数据,您很可能需要
useffect
,因为它不知道如何更新。已更新问题。const[SOR]是整个页面中使用的对象。最后一行是什么?
const[SOR]…
?请添加带有React类或功能组件的完整示例。我不清楚您在哪里使用上面的代码。您是否尝试调试您的逻辑?如果
SOR
依赖于
setResult
设置的数据,您很可能需要
useffect
,因为它不知道如何更新。已更新问题。const[SOR]是整个页面中使用的对象。
function getSOR(id) {
    if(id) {
        return axios.get(`${Constants.API_URL}/SOR?id=${id}`);
    } else {
        return axios.get(`${Constants.API_URL}/SOR`);
    }

}