【问题标题】:How to render multiple times with React useState如何使用 React useState 多次渲染
【发布时间】:2021-02-21 11:05:39
【问题描述】:

我正在尝试在 React 中定义一个函数,必须在该函数上多次呈现组件,因为我在函数本身内部多次更改状态。我知道 useState 钩子会在函数结束时重新渲染组件,那么如何在设置新状态后立即重新渲染呢?代码是这样的:

function App() {
  const [state, setState] = useState(0);

  const getServerState = () => {
    e.preventDefault();
    // Send something to the server and put the response on "response"
    if (response.customVar === "uploading") {
      setState(1);
      // Send another request to the server and wait for the response
      if (newResponse.customVar === "uploaded" && state === 1) {
        setState(2);
      } else {
        throw new Error("Error in uploading response");
      }
    } else {
      throw new Error("Error in response");
    }
  };

  const checkState() => {
    if (state === 0) return "Waiting for user click";
    else if (state === 1) return "Your file is being uploaded";
    else if (state === 2) return "Your file is ready!";
  };

  return (
    <div>
    {checkState()}
    <button onClick={getServerState}>
      Click me
    </button>
  </div>)
}

在上面的示例中,我没有得到任何重新渲染,因为状态更改为 1“为时已晚”,并且在状态为 1 时检查的 if 没有通过。这个问题有方法解决吗?我不能使用 useEffect 钩子,因为效果会更新状态,这会导致循环。

【问题讨论】:

  • 您是否尝试显示用户上传的状态?或者你想从服务器显示某些东西的状态?
  • 我不明白为什么你不能使用 useEffect。可能通过适当的条件,您可以实现这一目标。您已经知道要使用哪种获取策略了吗?
  • @JasonMcFarlane 第二个,为了简单起见,我省略了有关服务器的所有内容,因为我认为 React 中的多次重新渲染无关紧要。假设我首先在 URL 1 上发出 GET 请求,它给了我一定的响应,如果该响应是“上传”,我想通过重新渲染显示状态,然后立即发出第二个请求将根据我得到的响应生成第二次重新渲染
  • @Canta 那是因为如果我使用 useEffect 和 [state] 作为第二个参数并且效果是状态本身的更新(我会在第一次状态更改后调用它),它将继续调用useEffect 函数无限次
  • 因此,当您的组件挂载时,您的状态为 0,“等待用户点击”,然后当用户点击时,您的状态为 1“正在上传”或 2“文件准备就绪”?

标签: javascript reactjs components


【解决方案1】:

setState 后不能立即获取状态,取而代之的是,您需要定义一个临时布尔变量。

function App() {
  const [state, setState] = useState(0);

  const getServerState = () => {
    e.preventDefault();
    var isUploaded = false;
    // Send something to the server and put the response on "response"
    if (response.customVar === "uploading") {
      setState(1);
      isUploaded = true;
      // Send another request to the server and wait for the response
      if (newResponse.customVar === "uploaded" && isUploaded) {
        setState(2);
      } else {
        throw new Error("Error in uploading response");
      }
    } else {
      throw new Error("Error in response");
    }
  };

  const checkState() => {
    if (state === 0) return "Waiting for user click";
    else if (state === 1) return "Your file is being uploaded";
    else if (state === 2) return "Your file is ready!";
  };

  return (
    <div>
    {checkState()}
    <button onClick={getServerState}>
      Click me
    </button>
  </div>)
}

【讨论】:

  • 这无论如何都行不通,因为状态会立即变为 2,并且 checkState 函数不会打印状态 === 1 的情况。我需要打印所有这些,而不仅仅是第一个和最后一个。
  • 在您向服务器请求并获得状态 2 的响应之前,您也会看到状态 1
  • 您可以暂时勾选在 div 容器内添加 {state}。
  • 问题不在容器上,而在改变状态后的重新渲染上。因为 React 在函数完成之前不会重新渲染,所以不会得到 1 的状态
猜你喜欢
  • 2020-08-15
  • 2020-09-27
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-06-01
  • 2021-03-21
  • 1970-01-01
相关资源
最近更新 更多