Answers for "when to use useeffect in react"

0

useEffect react

function FriendStatusWithCounter(props) {
  const [count, setCount] = useState(0);
  useEffect(() => {    document.title = `You clicked ${count} times`;
  });

  const [isOnline, setIsOnline] = useState(null);
  useEffect(() => {    function handleStatusChange(status) {
      setIsOnline(status.isOnline);
    }

    ChatAPI.subscribeToFriendStatus(props.friend.id, handleStatusChange);
    return () => {
      ChatAPI.unsubscribeFromFriendStatus(props.friend.id, handleStatusChange);
    };
  });
  // ...
}
Posted by: Guest on June-25-2021
0

useEffectOnce

import {useEffectOnce} from 'react-use';

const Demo = () => {
  useEffectOnce(() => {
    console.log('Running effect once on mount')
    
    return () => {
      console.log('Running clean-up of effect on unmount')
    }
  });

  return null;
};
Posted by: Guest on May-16-2020

Code answers related to "Javascript"

Browse Popular Code Answers by Language