Skip to content

[URH-58] useOnlineStatus 신규 #47

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Aug 27, 2024
Merged

Conversation

suhyeoonn
Copy link
Contributor

@suhyeoonn suhyeoonn commented Aug 16, 2024

👾 Pull Request

  • 상태: 신규

1️⃣ Spec

  • 브라우저의 온라인/오프라인 상태를 판별하는 훅입니다.
  • 온라인 상태가 변경될 때 실행할 콜백 함수를 선택적으로 지정할 수 있습니다.

2️⃣ 변경 사항

  • 없음

3️⃣ 예시 코드

  1. 콜백이 필요 없는 경우
import useOnlineStatus from './hooks/useOnlineStatus';

function App() {
  const { isOnline } = useOnlineStatus();

  return (
    <div>
      <h1>{isOnline ? 'online' : 'offline'}</h1>
    </div>
  );
}
  1. 콜백이 필요한 경우
function App() {
  const { isOnline } = useOnlineStatus({ onlineCallback, offlineCallback });

  function onlineCallback() {
    console.log('online');
  }

  function offlineCallback() {
    console.log('offline');
  }

  return (
    <div>
      <h1>{isOnline ? 'online' : 'offline'}</h1>
    </div>
  );
}

4️⃣ 관련 문서 (선택 사항)

@Choozii
Copy link
Member

Choozii commented Aug 20, 2024

@suhyeoonn
수현님, 리액트 공식 문서 읽다가 수현님도 알면 좋을것 같은 부분을 발견해서 공유드립니다!
useSyncExternalStore라는 훅에 navigator.onLine 구독하는 코드가 예시로 있길래 공유드려욥!!!

https://ko.react.dev/reference/react/useSyncExternalStore#subscribing-to-a-browser-api

Copy link
Member

@jeongbaebang jeongbaebang left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍

🩹 chore: navigator 체크 함수를 util로 이동

🩹 chore:  콜백 함수 메모이제이션 적용하여 불필요한 리렌더링 방지

Revert "🩹 chore:  콜백 함수 메모이제이션 적용하여 불필요한 리렌더링 방지"

This reverts commit 83e6a7b.

📝 docs: 콜백 함수 메모이제이션 권장 내용 추가
@suhyeoonn suhyeoonn force-pushed the URH-58/use-online-status branch from c802471 to 9f373a2 Compare August 27, 2024 13:36
@suhyeoonn suhyeoonn merged commit 64d39e7 into master Aug 27, 2024
@suhyeoonn suhyeoonn deleted the URH-58/use-online-status branch August 27, 2024 13:37
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants