渲染组件在加载之前

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

Render component before loading

问题

我的组件在加载开始之前渲染了
大约1毫秒
我该如何修复它?

我使用redux toolkit

在登录后,我重定向到主页
在主页上,我获取数据并设置加载状态

builder.addCase(fetchRoot.fulfilled, (state, action) => {
    state.loading = false
})
builder.addCase(fetchRoot.pending, (state) => {
    state.loading = true
})
builder.addCase(fetchRoot.rejected, (state, action) => {
    state.loading = false
})

然后渲染我的组件

<div className='home-page'>
    {
        loading ?
            <Loader/>
            :
        <HomePage/>
    }
</div>

但它在加载完成之前迅速显示我的组件。

英文:

my component rendered before loading start
its about 1ms
how can i fix it?

i use redux toolkit

adter login i use redirect to main page
on main page i fetch data and set loading state


        builder.addCase(fetchRoot.fulfilled, (state, action) =&gt; {
            state.loading = false
        })
        builder.addCase(fetchRoot.pending, (state) =&gt; {
            state.loading = true
        })
        builder.addCase(fetchRoot.rejected, (state, action) =&gt; {
            state.loading = false
        })

then render my component


     &lt;div className=&#39;home-page&#39;&gt;
            {
                loading ?
                    &lt;Loader/&gt;
                    :
              &lt;HomePage/&gt;

            }
        &lt;/div&gt;

but it show rapidly my component before loading done

答案1

得分: 0

以下是您要翻译的内容:

我假设 API 请求是在 useEffect 中发起的,获取的数据被存储在 Redux 中,并通过 props 传递给组件。在这种情况下,最简单的方法是检查该属性是否未定义并进行安全返回。您可以返回 null 或一个加载指示器。如果您想要一个旋转器,请考虑有多少个组件会像这样操作,以及启动时会出现多少个旋转器。有时候 null 更好。

示例:

function UserProfile(props) {
  const {user} = props;

  useEffect(() => {
    API.getUserData();
  }, []);

  if (!user) return null; // 或者一个加载指示器

  return (
    <div className="UserProfile">
      <h1>{user.name}</h1>
    </div>
  );
}

这种解决方案的唯一缺点是当状态已经填充了先前的数据时,组件会进行第二次渲染。您需要决定是否可以接受先前的响应(甚至可能不进行第二次 API 请求)。或者,当组件卸载时,也许您想要丢弃旧数据。因此,第二次渲染从与第一次相同的 undefined 状态开始。

英文:

I assume that the API request is initiated in useEffect, fetched data are stored in redux and passed to the component in props. In this case the simplest way is to check if that property is not undefined and make a safe return. You can return null or a loading indicator. If you want a spinner, think how many components will act like that and how many spinners will appear upon startup. Sometimes null is better.

Example:

function UserProfile(props) {
  const {user} = props;

  useEffect(() =&gt; {
    API.getUserData();
  }, []);

  if (!user) return null; // or a loading indicator

  return (
    &lt;div className=&quot;UserProfile&quot;&gt;
      &lt;h1&gt;{user.name}&lt;/h1&gt;
    &lt;/div&gt;
  );
}

The only drawback of this solution is a second render of the component when state is already filled with previous data. You need to decide if the previous response is acceptable (maybe even not do a 2nd API request). Or maybe you might want to discard the old data when the component is unmounted. So the 2nd render starts with the same undefined state as the 1st one.

huangapple
  • 本文由 发表于 2023年5月11日 16:34:09
  • 转载请务必保留本文链接:https://go.coder-hub.com/76225646.html
匿名

发表评论

匿名网友

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

确定