medium
// this has nothing to do with question, it's a test question
const Abc = () => {
const [a, v] = useState("abc");
return (
<div>
new abc
dkkdkd
<span>
Nice working
</span>
</div>
)
}
Read the full question →harduseSyncExternalStore
// legacy pattern
const [snap, setSnap] = useState(store.getSnapshot());
useEffect(() => {
const unsub = store.subscribe(() => setSnap(store.getSnapshot()));
return unsub;
}, []);
Read the full question →easyfunction components
function Hi({name}){return <p>Hi {name}</p>} <Hi name="Alice"/>
Read the full question →mediumcontext and useContext
const ThemeContext = React.createContext('light');
function App() {
return <Child />; // no Provider anywhere
}
function Child() {
const theme = useContext(ThemeContext);
console.log(theme);
return null;
}
Read the full question →mediumReact Router v6
const [searchParams, setSearchParams] = useSearchParams();
const goToPage = (n) =>
setSearchParams({ page: String(n) });
Read the full question →easyclassName htmlFor
<label htmlFor="email">Email</label>
<input id="email" />
Read the full question →mediumuseEffect dependency arrays
useEffect(() => {
const sub = subscribe(id);
return () => {
console.log('cleanup');
sub.unsubscribe();
};
}, [id]);
Read the full question →mediumReact Router v6
createBrowserRouter([
{
path: '/',
element: <Root/>, // renders <Outlet/>
errorElement: <RootError/>,
children: [
{ path: 'reports', element: <Reports/>, loader: reportsLoader }, // loader throws
],
},
]);
Read the full question →mediumuseCallback
const onClick = useCallback(() => {
console.log(count);
}, []);
Read the full question →mediumuseEffect dependency arrays
const [count, setCount] = useState(0);
useEffect(() => {
console.log(count);
}, [count]);
return <button onClick={() => setCount(c => c + 1)}>+</button>;
Read the full question →hardstale-closure pitfalls
function Search({ query }) {
const run = useCallback(() => {
fetch(`/s?q=${query}`);
}, []); // query omitted
return <Child onRun={run} />;
}
Read the full question →mediumuseEffect dependency arrays
function Ticker() {
const [count, setCount] = useState(0);
useEffect(() => {
const id = setInterval(() => setCount(count + 1), 1000);
return () => clearInterval(id);
}, []);
return <span>{count}</span>;
}
Read the full question →mediumglobal state Zustand Redux
const active = useSelector((state) => state.items.filter((i) => i.active));
Read the full question →mediumcustom hooks
function useData(url) {
const [data, setData] = useState(null);
const refetch = () => fetch(url).then(r => r.json()).then(setData);
return { data, refetch };
}
Read the full question →mediumcontrolled vs uncontrolled
function Field() {
const [v, setV] = useState('');
return <input value={v} />;
}
Read the full question →mediumcustom hooks
function usePrevious(value) {
const ref = useRef();
useEffect(() => {
ref.current = value;
});
return ref.current;
}
Read the full question →mediumlazy and Suspense
<Suspense fallback={<Spinner />}>
<LazyA />
<LazyB />
</Suspense>
Read the full question →mediumforwardRef
const Input = forwardRef((props, ref) => {
// ref is forwarded here
return <input {...props} />;
});
Read the full question →mediumcustom hooks
function useFeature(enabled) {
if (enabled) {
const [value, setValue] = useState(0); // A
}
const [count, setCount] = useState(0); // B
useEffect(() => {}, []); // C
return count;
}
Read the full question →hardconcurrent rendering tearing
function handleChange(e) {
setText(e.target.value); // urgent
startTransition(() => {
setFilter(e.target.value); // transition (low priority)
});
}
Read the full question →