You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
When the context value changes, React will re-render the components using 'useContext(Context)'.
'use(Context)' can be called conditionally, the memorized components will be updated only when 'use(Context)' is called in the last rendering.
A bad example (This behavior does not seem to cause any problems in common usage)
importReact,{useState,useContext,use,useRef}from"react";constMyContext=React.createContext({value: 1});constCompUseCtx=React.memo(()=>{const{ value }=useContext(MyContext);return<div>useContext value {value}</div>;});constRandomUseContext=React.memo(()=>{constrenderCount=useRef(0);renderCount.current++;if(Math.random()>0.3){const{ value }=use(MyContext);return(<div>
use value {value} renderCount {renderCount.current}</div>);}return<div>renderCount {renderCount.current}</div>;});exportdefaultfunctionApp(){const[value,setValue]=useState(1);return(<MyContext.Providervalue={{ value }}>
value: {value}<buttononClick={()=>setValue((prev)=>prev+1)}>+1</button><CompUseCtx/><RandomUseContext/></MyContext.Provider>);}
The text was updated successfully, but these errors were encountered:
Summary
When the context value changes, React will re-render the components using 'useContext(Context)'.
'use(Context)' can be called conditionally, the memorized components will be updated only when 'use(Context)' is called in the last rendering.
Page
https://react.dev/reference/react/use#reading-context-with-use
Details
A bad example (This behavior does not seem to cause any problems in common usage)
The text was updated successfully, but these errors were encountered: