英文:
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<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>
);
};`
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<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>;
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论