useEffect
;deps
передаете пустой массив;const PageSize = () => {
const [pageSize, setPageSize] = useState([document.documentElement.scrollWidth, document.documentElement.scrollHeight]);
const updatePageSize = () => setPageSize([document.documentElement.scrollWidth, document.documentElement.scrollHeight]);
useEffect(() => {
window.addEventListener('resize', updatePageSize);
return () => {
window.removeEventListener('resize', updatePageSize);
};
}, []);
return (
<div>
<p>page width is {pageSize[0]}</p>
<p>page height is {pageSize[1]}</p>
</div>
);
};
typeof entity === 'object'
Правда тут есть одно исключение - в спеке нет типа function, а во всех реализациях он есть по историческим причинам. То есть с точки зрения спеки:typeof function(){} === 'object'
а на деле:typeof function(){} === 'function'
Поэтому правильное тождество будет такое:typeof entity === 'object' || typeof entity === 'function'
1 instanceof Number
1 instanceof Object
'a' instanceof String
'a' instanceof Object
true instanceof Boolean
true instanceof Object
1n instanceof BigInt
1n instanceof Object
Symbol() instanceof Symbol
Symbol() instanceof Object
null instanceof Object
undefined instanceof Object
все эти выражения ложны, а как известно instanceof проверяет именно прототипы.Object(1) instanceof Number
Object(1) instanceof Object
Object('a') instanceof String
Object('a') instanceof Object
Object(true) instanceof Boolean
Object(true) instanceof Object
Object(1n) instanceof BigInt
Object(1n) instanceof Object
Object(Symbol()) instanceof Symbol
Object(Symbol()) instanceof Object
все эти выражения истинны.typeof Object(1) === 'object'
typeof Object('a') === 'object'
typeof Object(true) === 'object'
typeof Object(1n) === 'object'
typeof Object(Symbol()) === 'object'
это все так же истинно.Object(null)
и Object(undefined)
возвращают просто пустой объект, обратится к методам null и undefined нельзя:null.property // TypeError: Cannot read property 'property' of null
undefined.property // TypeError: Cannot read property 'property' of undefined
Internal methods and internal slots are identified within this specification using names enclosed in double square brackets...
Within this specification a well-known symbol is referred to by using a notation of the form @@name...
Within this specification a reference such as %name% means the intrinsic object...
const CTX = React.createContext();
const App = () => (
<CTX.Provider value={ctx}>
<MyComponent />
</CTX.Provider>
);
const CTX = React.createContext(ctx);
const App = () => (
<MyComponent />
);
interface State {
shouldShowBtn: boolean;
}
class MyComponent extends React.Component<null, State> {
state = {
shouldShowBtn: true,
};
componentDidMount() {
window.addEventListener('scroll', this.scrollHandler);
}
scrollHandler = () => {
const { shouldShowBtn } = this.state;
if (window.pageYOffset > 50 && shouldShowBtn) {
this.setState({ shouldShowBtn: false });
} else if (window.pageYOffset <= 50 && !shouldShowBtn) {
this.setState({ shouldShowBtn: true });
}
};
componentWillUnmount() {
window.removeEventListener('scroll', this.scrollHandler);
}
render() {
return (
<>
{this.state.shouldShowBtn && <button>lioih </button>}
</>
);
}
}
const MyComponent: React.FC = () => {
const [shouldShowBtn, setShouldShowBtn] = useState(true);
const context = useMemo(() => ({ shouldShowBtn }), []);
useEffect(() => {
context.shouldShowBtn = shouldShowBtn;
}, [shouldShowBtn]);
useEffect(() => {
const scrollHandler = () => {
if (window.pageYOffset > 50 && context.shouldShowBtn) {
setShouldShowBtn(false);
} else if (window.pageYOffset <= 50 && !context.shouldShowBtn) {
setShouldShowBtn(true);
}
};
window.addEventListener('scroll', scrollHandler);
return () => {
window.removeEventListener('scroll', scrollHandler);
};
}, []);
return (
<>
{shouldShowBtn && <button>lioih </button>}
</>
);
};
const useGetState = (initialState: any) => {
const [state, _setState] = useState(initialState);
const context = useMemo(() => ({ state }), []);
const getState: any = useCallback(() => context.state, []);
const setState = useCallback((state) => {
context.state = state;
_setState(state);
}, []);
return [getState, setState];
};
const MyComponent: React.FC = () => {
const [getShouldShowBtn, setShouldShowBtn] = useGetState(true);
useEffect(() => {
const scrollHandler = () => {
console.log(window.pageYOffset, getShouldShowBtn());
if (window.pageYOffset > 50 && getShouldShowBtn()) {
setShouldShowBtn(false);
} else if (window.pageYOffset <= 50 && !getShouldShowBtn()) {
setShouldShowBtn(true);
}
}
window.addEventListener("scroll", scrollHandler);
return () => {
window.removeEventListener("scroll", scrollHandler);
};
}, []);
return <div>{getShouldShowBtn() && <button>lioih </button>}</div>;
};
const buttonEl = useRef<HTMLButtonElement>();
buttonEl.current.classList.add("is-hide");
return <button ref={buttonEl}>lioih</button>;
<script crossorigin src="https://unpkg.com/react@16/umd/react.production.min.js"></script>
<script crossorigin src="https://unpkg.com/react-dom@16/umd/react-dom.production.min.js"></script>
import React, { useContext } from 'react';