【问题标题】:ReactJS hooks: update state inside conditionReactJS 钩子:更新条件内的状态
【发布时间】:2022-06-22 18:48:46
【问题描述】:

大家!

有人能告诉我在 if / else 语句中更新状态是否可以被 React 规则接受。 特别是根据条件内的承诺结果更新状态。

例如:

import {useState} from "react";
import {somePromiseOne, somePromiseTwo} from "./example";

function example(){

  const [state, setState] = useState();

  if(true){
    somePromiseTwo().then((result) => setState(result));
  }else{
    somePromiseOne().then((result) => setState(result));
  }

}

【问题讨论】:

    标签: reactjs react-hooks


    【解决方案1】:

    在条件内使用状态挂钩是可以接受的。如果你想知道什么是 React 钩子的规则,你可以在这里阅读: https://reactjs.org/docs/hooks-rules.html

    【讨论】:

      猜你喜欢
      • 2017-11-05
      • 2014-05-14
      • 2016-10-02
      • 2021-05-19
      • 2020-02-12
      • 2020-08-20
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多