Answer the question
In order to leave comments, you need to log in
How does it work with a custom hook?
Please help me understand how it works?
https://ru.reactjs.org/docs/hooks-custom.html
The link above explains how to write custom hooks and the following hook is given as an example.
import { useState, useEffect } from 'react';
function useFriendStatus(friendID) {
const [isOnline, setIsOnline] = useState(null);
useEffect(() => {
function handleStatusChange(status) {
setIsOnline(status.isOnline);
}
ChatAPI.subscribeToFriendStatus(friendID, handleStatusChange);
return () => {
ChatAPI.unsubscribeFromFriendStatus(friendID, handleStatusChange);
};
});
return isOnline;
}
function FriendStatus(props) {
const isOnline = useFriendStatus(props.friend.id);
const [isOnline, setIsOnline] = useState(null);
Answer the question
In order to leave comments, you need to log in
Sorry, I just didn't see it right away. The answer was found here
https://ru.reactjs.org/docs/hooks-faq.html#how-doe...
react really binds hooks to components by keeping track of the order of useState calls. That is, knowing which component is being drawn at the moment, you can find out which hooks correspond to it. And next time, redraw only what you need.
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question