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: |

Start with a fresh view of investing strategy. The combination of risks and fads this quarter looks to be topping. That means the future is ready to move in.Likely, there will not be a wholesale shift. Company actions will aim to benefit from economic growth, inflationary pressures and a return of market-determined interest rates. In turn, all of that should drive the stock market and investment returns higher.

telegram from in


Telegram React
FROM USA