简体   繁体   English

如何使用 React 钩子更新程序函数的返回值?

[英]How to use the return value from React hook updater function?

I'm using the following code as an updater function for my state (inside hook).我使用以下代码作为我的状态(内部钩子)的更新程序函数。

While the console output works properly inside the callback, how can I access the value outside.虽然控制台输出在回调内部正常工作,但我如何访问外部的值。 I am returning someStatus and assign it to x .我正在返回someStatus并将其分配给x But x is printed as undefined .但是x打印为undefined

Is there some issue with the code below?下面的代码有问题吗?

let x = setSomeStatus((someStatus) => {
    console.log("Some Status :::: :" + someStatus); // "XYZ"
    return someStatus;
});
console.log("x = " + x); // undefined

I tried below as suggested, but still do not get updated value outside the callback;我按照建议在下面尝试,但仍然没有在回调之外获得更新的值;

setSomeStatus((someStatus) => {
    console.log("File Status :::: :" + someStatus); // "XYZ"
    return someStatus;
});
console.log("someStatus = " + someStatus);

BELOW SEEMS TO BE WORKING (Please confirm if it looks good)下面似乎可以正常工作(请确认它是否看起来不错)

const onCellClicked = (e) => {
        if (e.data.status !== 'TO') {
            console.log(e);
            //let fileStatus;
            let fileType = (e.data.status === 'Y') ? 'O' : 'I';
            setCurrentFileStatus(e.data.status);
            setFileType(fileType);

            var tempObj = {};
            setCurrentFileStatus((fileStatus) => {
                setFileType((fileType) => {
                    tempObj.currentFileStatus = fileStatus;
                    tempObj.summaryDate = form.dateField;
                    tempObj.fileType = fileType;
                });             
            });
            setSharedData(tempObj);
            console.log("tempObj = " + tempObj);
        }
    }

Assuming that setSomeStatus is a setState function , the return statement of the callback you pass to setState will just return from the callback, and consequently set the state, but not actually return a value from the setState function.假设setSomeStatus一个 setState 函数,则传递给 setState 的回调的 return 语句将仅从回调返回,并因此设置状态,但实际上不会从setState函数返回值。

As you anyways already have a state defined, you don't need to store it in a separate variable ( x in your case), you can access it directly.由于您已经定义了一个状态,因此您无需将其存储在单独的变量中(在您的情况下为x ),您可以直接访问它。

 const { useState } = React; const App = () => { const [someState, setSomeState] = useState(); return ( <div> <p>Type something to change the state</p> <input onInput={(e) => setSomeState((prev) => { console.log("changing the state from", prev, "to", e.target.value); return e.target.value; }) } /> <h2>The state is {someState}</h2> </div> ); }; // Render it ReactDOM.render(<App />, document.getElementById("root"));
 <script src="https://cdnjs.cloudflare.com/ajax/libs/react/17.0.1/umd/react.production.min.js"></script> <script src="https://cdnjs.cloudflare.com/ajax/libs/react-dom/17.0.1/umd/react-dom.production.min.js"></script> <div id="root"></div>

You mus call setSomeStatus with a new value:setSomeStatus使用新值调用setSomeStatus

const[someStatus, setSomeStatus] = useState('');

setSomeStatus(someStatus + x);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM