我尝试useState在这里寻找重置数组值,但找不到任何对数组值的引用。
尝试将下拉值从初始状态更改为 allowedState 值。我在这里使用 hooks 方法来设置使用setStateValues. 如果我注释该行代码,它会显示下拉列表。我不明白为什么我不能使用setStateValues方法来重置状态变量值。
我收到以下错误:
太多的重新渲染。React 限制渲染次数以防止无限循环
这里有什么问题吗?
    import React, { useState } from "react"; 
    import ReactDOM from "react-dom";
    const StateSelector = () => {   
    const initialValue = [
    { id: 0,value: " --- Select a State ---" }];
      const allowedState = [
        { id: 1, value: "Alabama" },
        { id: 2, value: "Georgia" },
        { id: 3, value: "Tennessee" }
        ];
      const [stateOptions, setStateValues] = useState(initialValue);  
      // initialValue.push(...allowedState);
      console.log(initialValue.length);
      setStateValues(allowedState); // Not sure why cannot I reset the state in here for an array.
         return (<div>
          <label>Select a State:</label>
          <select>
            {stateOptions.map((localState, index) => (
              <option key={localState.id}>{localState.value}</option>
            ))}
          </select>
        </div>   ); };
    const rootElement = document.getElementById("root");
    ReactDOM.render(<StateSelector />, rootElement);