admin管理员组文章数量:1386514
I'm confused why the isLoading
flag is set to true in the initial state. Isn't it supposed to be false initially? Consider this simple example:
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
Another thing it, if we set it to true
or false
initially in the above code, the useEffect
still runs the same and the result on the screen will also be the same. So, why true
? Is this some sort of standard?
I'm confused why the isLoading
flag is set to true in the initial state. Isn't it supposed to be false initially? Consider this simple example:
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
Another thing it, if we set it to true
or false
initially in the above code, the useEffect
still runs the same and the result on the screen will also be the same. So, why true
? Is this some sort of standard?
3 Answers
Reset to default 5The 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)
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/docs/handling-events.html
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 ponent actually is in such state initially and then you set it to false after fetched data is ready.
本文标签: javascriptWhy the initial loading state is set to true while using useStateStack Overflow
版权声明:本文标题:javascript - Why the initial loading state is set to true while using useState? - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744518558a2610313.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论