Gathering detailed insights and metrics for @fluentui/react-context-selector
Gathering detailed insights and metrics for @fluentui/react-context-selector
Gathering detailed insights and metrics for @fluentui/react-context-selector
Gathering detailed insights and metrics for @fluentui/react-context-selector
Fluent UI web represents a collection of utilities, React components, and web components for building web applications.
npm install @fluentui/react-context-selector
Typescript
Module System
Node Version
NPM Version
@fluentui/react-monaco-editor v1.7.348
Updated on Jul 11, 2025
@fluentui/react-docsite-components v8.13.230
Updated on Jul 11, 2025
@fluentui/react-charting v5.24.12
Updated on Jul 11, 2025
@fluentui/chart-utilities v1.1.21
Updated on Jul 11, 2025
@fluentui/codemods v8.4.28
Updated on Jul 10, 2025
@fluentui/react-tooltip v9.7.6
Updated on Jul 07, 2025
TypeScript (95%)
JavaScript (2.07%)
MDX (1.96%)
SCSS (0.59%)
HTML (0.21%)
CSS (0.17%)
Shell (0.01%)
Total Downloads
0
Last Day
0
Last Week
0
Last Month
0
Last Year
0
NOASSERTION License
19,397 Stars
19,659 Commits
2,813 Forks
286 Watchers
108 Branches
908 Contributors
Updated on Jul 12, 2025
Latest Version
9.2.2
Package Id
@fluentui/react-context-selector@9.2.2
Unpacked Size
107.57 kB
Size
16.34 kB
File Count
25
NPM Version
10.8.2
Node Version
20.19.0
Published on
Jun 26, 2025
Cumulative downloads
Total Downloads
Last Day
0%
NaN
Compared to previous day
Last Week
0%
NaN
Compared to previous week
Last Month
0%
NaN
Compared to previous month
Last Year
0%
NaN
Compared to previous year
2
5
2
@fluentui/react-context-selector
React useContextSelector()
hook in userland.
React Context and useContext()
is often used to avoid prop drilling,
however it's known that there's a performance issue. When a context value is changed, all components that are subscribed with useContext()
will re-render.
useContextSelector is recently proposed. While waiting for the process, this library provides the API in userland.
NPM
1npm install --save @fluentui/react-context-selector
Yarn
1yarn add @fluentui/react-context-selector
1import * as React from 'react'; 2import { createContext, useContextSelector, ContextSelector } from '@fluentui/react-context-selector'; 3 4interface CounterContextValue { 5 count1: number; 6 count2: number; 7 incrementCount1: () => void; 8 incrementCount2: () => void; 9} 10 11// 💡 The same syntax as native React context API 12// https://reactjs.org/docs/context.html#reactcreatecontext 13const CounterContext = createContext<CounterContextValue>({} as CounterContextValue); 14 15const CounterProvider = CounterContext.Provider; 16 17// not necessary but can be a good layer to mock for unit testing 18const useCounterContext = <T,>(selector: ContextSelector<CounterContextValue, T>) => 19 useContextSelector(CounterContext, selector); 20 21const Counter1 = () => { 22 // 💡 Context updates will be propagated only when result of a selector function will change 23 // "Object.is()" is used for internal comparisons 24 const count1 = useCounterContext(context => context.count1); 25 const increment = useCounterContext(context => context.incrementCount1); 26 27 return <button onClick={increment}>Counter 1: {count1}</button>; 28}; 29 30const Counter2 = () => { 31 const count2 = useCounterContext(context => context.count2); 32 const increment = useCounterContext(context => context.incrementCount2); 33 34 return <button onClick={increment}>Counter 2: {count2}</button>; 35}; 36 37export default function App() { 38 const [state, setState] = React.useState({ count1: 0, count2: 0 }); 39 40 const incrementCount1 = React.useCallback(() => setState(s => ({ ...s, count1: s.count1 + 1 })), [setState]); 41 const incrementCount2 = React.useCallback(() => setState(s => ({ ...s, count2: s.count2 + 1 })), [setState]); 42 43 return ( 44 <div className="App"> 45 <CounterProvider 46 value={{ 47 count1: state.count1, 48 count2: state.count2, 49 incrementCount1, 50 incrementCount2, 51 }} 52 > 53 <Counter1 /> 54 <Counter2 /> 55 </CounterProvider> 56 </div> 57 ); 58}
This helper hook will allow you to know if a component is wrapped by a context selector provider
1const Foo = () => { 2 // An easy way to test if a context provider is wrapped around this component 3 // since it's more complicated to compare with a default context value 4 const isWrappedWithContext = useHasParentContext(CounterContext); 5 6 if (isWrappedWithContext) { 7 return <div>I am inside context selector provider</div>; 8 } else { 9 return <div>I can only use default context value</div>; 10 } 11};
React context by nature triggers propagation of component re-rendering if a value is changed. To avoid this, this library uses undocumented feature of calculateChangedBits
. It then uses a subscription model to force update when a component needs to re-render.
children
of a context provider has to be either created outside of the provider or memoized with React.memo
.<Consumer />
components are not supported.The implementation is heavily inspired by:
No vulnerabilities found.
Reason
30 commit(s) out of 30 and 22 issue activity out of 30 found in the last 90 days -- score normalized to 10
Reason
no vulnerabilities detected
Reason
update tool detected
Details
Reason
security policy file detected
Details
Reason
license file detected
Details
Reason
no binaries found in the repo
Reason
GitHub code reviews found for 28 commits out of the last 30 -- score normalized to 9
Details
Reason
branch protection is not maximal on development and all release branches
Details
Reason
dependency not pinned by hash detected -- score normalized to 5
Details
Reason
no badge detected
Reason
dangerous workflow patterns detected
Details
Reason
non read-only tokens detected in GitHub workflows
Details
Reason
project is not fuzzed
Score
Last Scanned on 2022-08-15
The Open Source Security Foundation is a cross-industry collaboration to improve the security of open source software (OSS). The Scorecard provides security health metrics for open source projects.
Learn More