useEffect 在下一个命令之后执行

huangapple go评论60阅读模式
英文:

useEffect executing after (?) the next command

问题

A problem here useEffect 在下一个命令之后执行

我在一个函数组件中有这段代码:

const [fact, setFact] = useState();
      
useEffect(() => {

  async function startGetCatFact() {
    const { fact: factFromApi } = await getCatFact();
    setFact(factFromApi);
  }

  startGetCatFact();

  return;
}, []);

const queryWords = fact.split(' ').slice(0, 3); // 出现 "fact is not defined" 错误

但我无法理解这里的问题:

当我执行这段代码时,出现一个错误,指出 "fact" 未定义。
useEffect应该设置我的状态 "fact",我应该能够在使用useEffect后使用它,但我始终获得未定义的错误。
我相当肯定这与异步过程和其他内容有关,但我找不到解决方案。

有何想法?

英文:

A problem here useEffect 在下一个命令之后执行

I have this code inside a functional component:

<!-- begin snippet: js hide: false console: true babel: false -->

<!-- language: lang-js -->

  const [fact, setFact] = useState();
  
  useEffect( () =&gt; {

    async function startGetCatFact () {
      const {fact:factFromApi} = await getCatFact();
      setFact(factFromApi);
    }

    startGetCatFact();

    return 
  },[])
  
   
  const queryWords = fact.split(&#39; &#39;).slice(0, 3); //fact is not defined error

<!-- end snippet -->

But there's a problem here that I can't understand:

When I execute this code I get an error that says that "fact" is not defined.
UseEffect should set my state fact and I should be able to use it after using the useEffect but I get the undefined all the time.
Pretty sure this is about asynchronous process and stuff but I can't find a solution.

Any thoughts?

答案1

得分: 1

it's very straightforward - when the code first gets uploaded, way before the useEffect runs - the 'fact' variable has no value, so it crashes.

the hook useEffect is basically the same as componentDidMount, so it only runs after the component mounted already - which means the code that uses the 'fact' variable first takes it's value from the initial state you're giving it when you're defining it.

to create an easy fix you can just give it a value in the use state and when the component will mount - that value will change to whatever you do set it to in the useEffect

英文:

it's very straightforward - when the code first gets uploaded, way before the useEffect runs - the 'fact' variable has no value, so it crashes.

the hook useEffect is basically the same as componentDidMount, so it only runs after the component mounted already - which means the code that uses the 'fact' variable first takes it's value from the initial state you're giving it when you're defining it.

to create an easy fix you can just give it a value in the use state and when the component will mount - that value will change to whatever you do set it to in the useEffect

答案2

得分: 1

将您的函数放在useEffect之外,并在其中调用。

const [fact, setFact] = useState();
const [loading, setLoading] = useState(true);

const startGetCatFact = async () => {
  const { fact: factFromApi } = await getCatFact();
  setFact(factFromApi);
  setLoading(false);
};

useEffect(() => {
  startGetCatFact();
}, []);

if (!loading) {
  const queryWords = fact.split(' ').slice(0, 3);
}

这样,您将完全控制加载阶段。

英文:

create your function outside of useEffect and call inside

const [fact, setFact] = useState();
const [loading,setLoading] = useState(true);
const startGetCatFact = async () =&gt; {
  const {fact:factFromApi} = await getCatFact();
  setFact(factFromApi);
  setLoading(false)
}

useEffect( () =&gt; {
    startGetCatFact();
  },[])

if(!loading){
  const queryWords = fact.split(&#39; &#39;).slice(0, 3);
}

so in that way u will fully control the loading stage.

答案3

得分: -1

替换此部分:

const queryWords = fact.split(' ').slice(0, 3);

为:

const queryWords = fact?.split(' ').slice(0, 3);

它会在第一次渲染时检查变量,以及当变量可用时将使用该变量。

英文:

replace this:

const queryWords = fact.split(&#39; &#39;).slice(0, 3);

with:

const queryWords = fact?.split(&#39; &#39;).slice(0, 3);

It(?) will to check variable on first render and when the variable is available it will use the variable

huangapple
  • 本文由 发表于 2023年2月23日 19:48:25
  • 转载请务必保留本文链接:https://go.coder-hub.com/75544422.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定