componentDidMount() javascript Copy useEffect(() => {
/* componendDidMount function content */
}, []);
Function, that works like `componentDidMount()` in class-based components.
componentDidUpdate() javascript Copy const mounted = useRef();
useEffect(() => {
if (!mounted.current) {
mounted.current = true;
return;
}
/* componentDidUpdate function content */
}, []);
Function, that works like `componentDidUpdate()` in class-based components.
componentDidMount() + componentDidUpdate() javascript Copy useEffect(() => {
/* componendDidMount + componendDidUpdate function content */
});
Function, that works like combination of `componentDidMount()` and `componentDidUpdate()` in class-based components.
componentWillUnmount() javascript Copy useEffect(() => {
return () => {
/* componentWillUnmount function content */
}
}, []);
Function, that works like `componentWillUnmount()` in class-based components.
forceUpdate() javascript Copy const [ignored, forceUpdate] = useReducer(x => x + 1, 0);
Function, that works like `this.forceUpdate()` in class-based components.
useClientRect javascript Copy function useClientRect() {
const [rect, setRect] = useState(null);
const ref = useCallback(node => {
if (node !== null) {
setRect(node.getBoundingClientRect());
}
}, []);
return [rect, ref];
}
Measure the element on the first rendering.
See example useImageClientRect javascript Copy function useImageClientRect() {
const [rect, setRect] = useState({});
const ref = useCallback(node => {
if (node !== null) {
node.addEventListener("load", () => {
setRect(node.getBoundingClientRect());
});
}
}, []);
return [rect, ref];
}
Measure the image element after its first loading into the DOM.
See example useClientRectObserver javascript Copy function useClientRectObserver() {
const ref = useRef();
const [rect, setRect] = useState(null);
useEffect(() => {
const resizeObserver = new ResizeObserver(entries => {
if (!Array.isArray(entries) || !entries.length) {
return;
}
setRect(entries[0].contentRect);
});
resizeObserver.observe(ref.current);
return () => resizeObserver.unobserve(ref.current);
}, []);
return [rect, ref];
}
Measure the element on the first rendering and every resize. That hook uses an experimental ResizeObserver. If you care about backwards compatibility use [resize-observer-polyfill](https://www.npmjs.com/package/resize-observer-polyfill).
See example useFetch javascript Copy function useFetch(url, initialValue) {
const [data, setData] = useState(initialValue);
const [loading, setLoading] = useState(true);
const [error, setError] = useState(null);
useEffect(() => {
setLoading(true);
fetch(url)
.then(res => res.json())
.then(setData)
.catch(setError)
.finally(() => setLoading(false));
}, [url]);
return [data, loading, error];
}
Use browser Fetch API to send a request to the provided URL.
See example usePrevious javascript Copy function usePrevious(value) {
const ref = useRef();
useEffect(() => {
ref.current = value;
});
return ref.current;
}
Access to the previous state/props of the component.
See example