为什么组件使用上下文(context)时总是重新渲染?

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

Why component using context, always re-render?

问题

这是您要翻译的内容:

我有一个带上下文的问题

下面是我的代码

const { getValueFromKey, setValueToLocal, removeValueStorage } = useStorage<AuthStateValue>();
const [state, setState] = useState<AuthStateValue>({ ...initialValue });

const handleLogin = () => {
const dataFromLocal = getValueFromKey('user-info');
setState({ ...dataFromLocal, isAuth: true });
setValueToLocal('access-token', state.accessToken);
};

const handleLogout = () => {
setState(initialValue);
removeValueStorage('user-info');
};

const userInfo = useMemo(() => ({ ...state }), [state]);
const login = useCallback(() => handleLogin(), [handleLogin]);
const logout = useCallback(() => handleLogout(), [handleLogout]);

return (
<AuthContext.Provider value={{ state: { ...userInfo }, login, logout }}>
{children}
</AuthContext.Provider>
};

在我的组件中

const { login, state } = useContext(AuthContext);

当我使用上下文时,尽管进行了记忆化处理,但子组件仍然会重新渲染。有人可以帮我解释吗?

帮我澄清React中的上下文和记忆化。

英文:

I have a question with context

below this is my code

 const { getValueFromKey, setValueToLocal, removeValueStorage } = useStorage&lt;AuthStateValue&gt;();
 const [state, setState] = useState&lt;AuthStateValue&gt;({ ...initialValue });

 const handleLogin = () =&gt; {
   const dataFromLocal = getValueFromKey(&#39;user-info&#39;);
   setState({ ...dataFromLocal, isAuth: true });
   setValueToLocal(&#39;access-token&#39;, state.accessToken);
 };

 const handleLogout = () =&gt; {
   setState(initialValue);
   removeValueStorage(&#39;user-info&#39;);
 };

 const userInfo = useMemo(() =&gt; ({ ...state }), [state]);
 const login = useCallback(() =&gt; handleLogin(), [handleLogin]);
 const logout = useCallback(() =&gt; handleLogout(), [handleLogout]);

 return (
   &lt;AuthContext.Provider value={{ state: { ...userInfo }, login, logout }}&gt;
      {children}
   &lt;/AuthContext.Provider&gt;
 );
};`

in my component

const { login, state } = useContext(AuthContext);

when I using context, in spite of memoization value, but child component always re-render. Can somebody help me explain?

help me clarify context and memoization in react

答案1

得分: 1

以下是翻译好的部分:

You need to memoize the entire value you pass to the provider so its identity doesn't change; something like this.

The functions are also memoized (with useCallback) so they don't change every render either.

function Component() {
  const { getValueFromKey, setValueToLocal, removeValueStorage } = useStorage<AuthStateValue>();
  const [state, setState] = useState<AuthStateValue>({ ...initialValue });

  const handleLogin = useCallback(() => {
    const dataFromLocal = getValueFromKey("user-info");
    setState({ ...dataFromLocal, isAuth: true });
    setValueToLocal("access-token", state.accessToken);
  }, [/* TODO */]);

  const handleLogout = useCallback(() => {
    setState(initialValue);
    removeValueStorage("user-info");
  }, [/* TODO */]);

  const value = useMemo(() => ({ state, login, logout }), [state, login, logout]);

  return <AuthContext.Provider value={value}>{children}</AuthContext.Provider>;
}

希望这对你有所帮助。

英文:

You need to memoize the entire value you pass to the provider so its identity doesn't change; something like this.

The functions are also memoized (with useCallback) so they don't change every render either.

function Component() {
  const { getValueFromKey, setValueToLocal, removeValueStorage } = useStorage&lt;AuthStateValue&gt;();
  const [state, setState] = useState&lt;AuthStateValue&gt;({ ...initialValue });

  const handleLogin = useCallback(() =&gt; {
    const dataFromLocal = getValueFromKey(&quot;user-info&quot;);
    setState({ ...dataFromLocal, isAuth: true });
    setValueToLocal(&quot;access-token&quot;, state.accessToken);
  }, [/* TODO */]);

  const handleLogout = useCallback(() =&gt; {
    setState(initialValue);
    removeValueStorage(&quot;user-info&quot;);
  }, [/* TODO */]);

  const value = useMemo(() =&gt; ({ state, login, logout }), [state, login, logout]);

  return &lt;AuthContext.Provider value={value}&gt;{children}&lt;/AuthContext.Provider&gt;;
}

huangapple
  • 本文由 发表于 2023年4月10日 18:37:47
  • 转载请务必保留本文链接:https://go.coder-hub.com/75976345.html
匿名

发表评论

匿名网友

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

确定