為什麼在使用 useState 時初始加載狀態設置為 true? (Why the initial loading state is set to true while using useState?)


問題描述

為什麼在使用 useState 時初始加載狀態設置為 true? (Why the initial loading state is set to true while using useState?)

我很困惑為什麼 isLoading 標誌在初始狀態下設置為 true。它最初不應該是 false 嗎?考慮這個簡單的例子:

const SearchCharity = () => {
  const [isLoading, setIsLoading] = useState(true)
  const [themes, setThemes] = useState([])

  useEffect(() => {
    const fetchThemes = async () => {
      try {
        setIsLoading(true)

        const result = await axios.get(url)
        setThemes(result.data.themes.theme)

        setIsLoading(false)
      } catch (err) {
        console.log(err)
      }
    }
    fetchThemes()
  }, [])

  return (
      {
        isLoading ? <h1>Loading......</h1> : <h1>Display Content</h1>
      }
    )

export default SearchCharity

另外一件事,如果我們在上面的代碼中最初將它設置為 truefalseuseEffect 仍然運行相同,屏幕上的結果也將相同。那麼,為什麼是 true?這是某種標準嗎?


參考解法

方法 1:

The value you pass when initializing state is up to you.

If you want it to be initialized as true you pass true like this.

const [isLoading, setIsLoading] = useState(true)

If you want you can also pass false and it will be initialized as false

const [isLoading, setIsLoading] = useState(false)

方法 2:

  const [isLoading, setIsLoading] = useState(true)
  const [themes, setThemes] = useState([])

  useEffect(() => {
    const fetchThemes = async () => {
      try {
        const result = await axios.get(url)
        setThemes(result.data.themes.theme)

        setIsLoading(false)
      } catch (err) {
        console.log(err)
      }
    }
    fetchThemes()
  }, [])

  return (
      {
        isLoading ? <h1>Loading......</h1> : <h1>Display Content</h1>
      }
    )

export default SearchCharity

More info: https://reactjs.org/docs/handling‑events.html

方法 3:

You are asking about the reason that you did set this initial value yourself :)

Most certainly you copied that code from somewhere and the reason the isLoading's initial state is set to true is that your whole component actually is in such state initially and then you set it to false after fetched data is ready.

(by user13618351Shmili BreuerStanley Pierre Louisk‑wasilewski)

參考文件

  1. Why the initial loading state is set to true while using useState? (CC BY‑SA 2.5/3.0/4.0)

#use-state #react-hooks #javascript #use-effect #reactjs






相關問題

為什麼在使用 useState 時初始加載狀態設置為 true? (Why the initial loading state is set to true while using useState?)

如何在 React 中提取狀態的值 (How to extract value of a state in React)

如何在使用狀態掛鉤更新狀態時覆蓋具有相同鍵的對象 (How can overwrite objects that have the same key in updating State with state hooks)

如何使用 useState 將許多元素添加到數組中? (How to add many elements to an array with useState?)

如何正確附加到在功能組件中作為道具傳遞的字符串數組 (How to properly append to string array that was passed as props in functional component)

useState 在功能上與組件 React 的行為不同 (useState behaves differently in function vs componenet React)

如何在反應鉤子中設置多個值來狀態 (How to set multiple values to state in react hooks)

在 reactJS 中使用 useState 和外部過濾器 (using useState and External Filters in reactJS)

React.js 更改下拉值 (React.js to change dropdown value)

useState 不會立即更新狀態 (useState is not updating state immediately)

使用 useState 時,Reactjs 不會立即更新分配的值 (Reactjs doesn't immediately update the assigned value when useState is used)

如何為 useState 創建動態函數名稱? (How do I create a dynamic function name for useState?)







留言討論