Telegram Group & Telegram Channel
useContextSelector: Ускорение работы React-приложений

Случалось ли вам иметь контекст, который был настолько велик, что негативно влиял на производительность вашего React-приложения? Это происходит потому, что одно изменение значения контекста приводит к рендерингу всех компонентов, зависящих от этого контекста.

Классический пример - контекст темы, хранящий все цвета, шрифты и другие стили пользовательского интерфейса.


import { createContext, useState } from 'react';

const defaultTheme = { color: '#aabbcc', fontFamily: 'Arial', fontSize: 16 };

export const ThemeContext = createContext(defaultTheme);

export const ThemeContextProvider = ({ value = defaultTheme, children }) => {
const [theme, setTheme] = useState(value);
return (
<ThemeContext.Provider value={{ theme, setTheme }}>
{children}
</ThemeContext.Provider>
);
};

export const useTheme = () => {
const { theme } = useContext(ThemeContext);
return theme;
}


https://marmelab.com/blog/2024/10/16/usecontextselector-a-faster-usecontext-for-react.html

✍️ @React_lib



tg-me.com/React_lib/613
Create:
Last Update:

useContextSelector: Ускорение работы React-приложений

Случалось ли вам иметь контекст, который был настолько велик, что негативно влиял на производительность вашего React-приложения? Это происходит потому, что одно изменение значения контекста приводит к рендерингу всех компонентов, зависящих от этого контекста.

Классический пример - контекст темы, хранящий все цвета, шрифты и другие стили пользовательского интерфейса.


import { createContext, useState } from 'react';

const defaultTheme = { color: '#aabbcc', fontFamily: 'Arial', fontSize: 16 };

export const ThemeContext = createContext(defaultTheme);

export const ThemeContextProvider = ({ value = defaultTheme, children }) => {
const [theme, setTheme] = useState(value);
return (
<ThemeContext.Provider value={{ theme, setTheme }}>
{children}
</ThemeContext.Provider>
);
};

export const useTheme = () => {
const { theme } = useContext(ThemeContext);
return theme;
}


https://marmelab.com/blog/2024/10/16/usecontextselector-a-faster-usecontext-for-react.html

✍️ @React_lib

BY React




Share with your friend now:
tg-me.com/React_lib/613

View MORE
Open in Telegram


telegram Telegram | DID YOU KNOW?

Date: |

A project of our size needs at least a few hundred million dollars per year to keep going,” Mr. Durov wrote in his public channel on Telegram late last year. “While doing that, we will remain independent and stay true to our values, redefining how a tech company should operate.

telegram from it


Telegram React
FROM USA