React Hook “useEffect” is called conditionally

后端 未结 4 2210
再見小時候
再見小時候 2020-12-15 04:53

React is complaining about code below, saying it useEffect is being called conditionally:

相关标签:
4条回答
  • 2020-12-15 05:01

    I would argue there is a way to call hooks conditionally. You just have to export some members from that hook. Copy-paste this snippet in codesandbox:

    import React from "react";
    import ReactDOM from "react-dom";
    
    function useFetch() {
      return {
        todos: () =>
          fetch("https://jsonplaceholder.typicode.com/todos/1").then(response =>
            response.json()
          )
      };
    }
    
    const App = () => {
      const fetch = useFetch(); // get a reference to the hook
    
      if ("called conditionally") {
        fetch.todos().then(({title}) => 
          console.log("it works: ", title)); // it works:  delectus aut autem  
      }
    
      return null;
    };
    
    const rootElement = document.getElementById("root");
    ReactDOM.render(<App />, rootElement);
    
    0 讨论(0)
  • 2020-12-15 05:04

    Your code, after an if statement that contains return, is equivalent to an else branch:

    if(!firebase.getCurrentUsername()) {
        ...
        return null
    } else {
        useEffect(...)
        ...
    }
    

    Which means that it's executed conditionally (only when the return is NOT executed).

    To fix:

    useEffect(() => {
      if(firebase.getCurrentUsername()) {
        firebase.getCurrentUserQuote().then(setQuote)
      }
    }, [firebase.getCurrentUsername(), firebase.getCurrentUserQuote()])
    
    if(!firebase.getCurrentUsername()) {
      ...
      return null
    }
    
    0 讨论(0)
  • 2020-12-15 05:05

    Don’t call Hooks inside loops, conditions, or nested functions. Instead, always use Hooks at the top level of your React function. You can follow the documentation here.

    I couldn't find the use case in the above code. If you need the effect to run when the return value of firebase.getCurrentUsername() changes, you might want to use it outside the if condition like:

    useEffect(() => {
        firebase.getCurrentUserQuote().then(setQuote)
    }, [firebase.getCurrentUsername()]);
    
    0 讨论(0)
  • 2020-12-15 05:19

    The issue here is that when we are returning null from the if block, the useEffect hook code will be unreachable, since we returned before it, and hence the error that it is being called conditionally.

    You might want to define all the hooks first and then start writing the logic for rendering, be it null or empty string, or a valid JSX.

    0 讨论(0)
提交回复
热议问题