// MyContext.js
const Context = createContext(null);
const Provider = ({children}) => {
return (
<Context.Provider value={/* your state */}>{children}</Context.Provider>
)
}
const useYourValue = () => {
const value = useContext(Context);
if (!value) { /* handle case when context is used outside of Context provider */ }
return value
}
export {useYourValue, Provider}
// App.js
...
return (
<Provider>
<ComponentA />
{/* other components */ }
</Provider>
);
// ComponentA.js
import {useYourValue} from '../Context'
const ComponentA = () => {
const myValue = useYourValue();
// rest of component code
}
if (notAlreadyExecuted) { ... } // do something
...
<input checked={index === 0} {...props} />
...
type ComponentProps = {
prop1: string;
prop2: number;
prop3: boolean;
}
export const Component = ({ prop1, prop2, prop3 }: ComponentProps) => {
...
}
export const Component: FC<ComponentProps> = ({ prop1, prop2, prop3 }) => {
...
}
const [screenW, setScreenW] = useState(undefined);
useEffect(() => {
setScreenW(window.innerWidth);
}, []);
const Component = () => {
...
return (
...
{screenW >= 1024 && <BigScreenComponent />}
<SomeOtherComponent />
{screenW < 1024 && <SmallScreenComponent />}
...
);
interface MyLittleInterface {
age: number;
name: string;
}
const someVar: MyLittleInterface = {}; // ой-ой!
const someOtherVar: MyLittleInterface = {} as MyLittleInterface; // Ура!
const [shouldCallApi, setShouldCallApi] = useState(false);
useEffect(() => {
const timerId = setTimeout(() => {
setShouldCallApi(true);
}, 1000);
return () => clearTimeout(timerId);
}, [clientName]);
const { data, isLoading } = useQuery(['task-client-list', clientName1], () => loadClientList(clientName),{enabled: shouldCallApi});
const [state, setState] = useState(undefined);
const handleChangeState = (newState) => {
setState(newState);
}
return (
<Component handler={handleChangeState} />
);
...
function Component({handler}) {
return (
<input onChange={(e) => handler(e.target.files)} />
);
const [isMobile, setIsMobile] = useState(undefined);
const [state,setState] = useState();
useEffect(() => {
if (typeof window !== 'undefined')
setState(...)
}