如何在 React 中提取狀態的值 (How to extract value of a state in React)


問題描述

如何在 React 中提取狀態的值 (How to extract value of a state in React)

在我的 App.jsx 中使用多個狀態變量時,我想將一個狀態的值設置為等於另一個狀態的值。

const [exchangeRatio, setExchangeRatio] = useState();
const [toCurValue, setToCurValue] = useState();

function foo()
{
   setToCurValue(exchangeRatio);
}

在 console.log(exchangeRatio );

{exchangeRatio: undefined}
exchangeRatio: undefined
__proto__:
constructor: ƒ Object()
hasOwnProperty: ƒ hasOwnProperty()
isPrototypeOf: ƒ isPrototypeOf()
propertyIsEnumerable: ƒ propertyIsEnumerable()
toLocaleString: ƒ toLocaleString()
toString: ƒ toString()
valueOf: ƒ valueOf()
__defineGetter__: ƒ __defineGetter__()
__defineSetter__: ƒ __defineSetter__()
__lookupGetter__: ƒ __lookupGetter__()
__lookupSetter__: ƒ __lookupSetter__()
get __proto__: ƒ __proto__()
set __proto__: ƒ __proto__()

進一步

console.log({exchangeRatio}.valueOf());
>>Undefined

如果有辦法做同樣的事情,我是React的新手,請隨意寫。


參考解法

方法 1:

const [exchangeRatio, setExchangeRatio] = useState();
const [toCurValue, setToCurValue] = useState();

The above statement denotes that you are declaring a state with values stored in exchangeRatio and toCurValue respectively and those states can be modified only using setExchangeRatio and setToCurValue methods respectively.

While you declare a state using useState(), you are expected to specify the initial value of the state inside the useState() as a parameter. For eg. if you want the initial value of exchangeRatio to be 10, then you must write it as ‑

const [exchangeRatio, setExchangeRatio] = useState(10);

Now, since you haven't provided an initial state value, you are getting the console logged output as undefined. Once, you properly initialize it with the proper value, you can directly use the state and the stored value will be returned to you.


So, suppose you want to set toCurValue's value to be equal to exchangeRatio's value (which has been initialised properly), then you can either do it while writing useState() or using the setToCurValue in following ways ‑

const [toCurValue, setToCurValue] = useState(exchangeRatio);

OR

setToCurValue(exchangeRatio);

(by nutNCrackerAbhishek Bhagate)

參考文件

  1. How to extract value of a state in React (CC BY‑SA 2.5/3.0/4.0)

#use-state #react-hooks #reactjs #react-state






相關問題

為什麼在使用 useState 時初始加載狀態設置為 true? (Why the initial loading state is set to true while using useState?)

如何在 React 中提取狀態的值 (How to extract value of a state in React)

如何在使用狀態掛鉤更新狀態時覆蓋具有相同鍵的對象 (How can overwrite objects that have the same key in updating State with state hooks)

如何使用 useState 將許多元素添加到數組中? (How to add many elements to an array with useState?)

如何正確附加到在功能組件中作為道具傳遞的字符串數組 (How to properly append to string array that was passed as props in functional component)

useState 在功能上與組件 React 的行為不同 (useState behaves differently in function vs componenet React)

如何在反應鉤子中設置多個值來狀態 (How to set multiple values to state in react hooks)

在 reactJS 中使用 useState 和外部過濾器 (using useState and External Filters in reactJS)

React.js 更改下拉值 (React.js to change dropdown value)

useState 不會立即更新狀態 (useState is not updating state immediately)

使用 useState 時,Reactjs 不會立即更新分配的值 (Reactjs doesn't immediately update the assigned value when useState is used)

如何為 useState 創建動態函數名稱? (How do I create a dynamic function name for useState?)







留言討論