facebook / relay

Relay is a JavaScript framework for building data-driven React applications.
https://relay.dev
MIT License
18.39k stars 1.82k forks source link

What is the best way to manage relay environment at logging out. #3104

Open devethan opened 4 years ago

devethan commented 4 years ago

Hi all. I'm in making relay-boilerplate now. It uses relay-hooks, RelayEnvironmentProvider in order to set a relay environment.

Previously, I faced the problem resetting relay environment instance on authentication flow but I found the way is managed by the global state on the provider.

Here's my code.

// App.tsx
function RelayEnvironmentWrapper({ children }): ReactElement {
  // relay is managed by AuthContext
  const {
    state: { relay },
  } = useAuthContext();
  return (
    <RelayEnvironmentProvider environment={relay}>
      <ErrorBoundary fallback={<SuspenseScreen error />}>
        <Suspense fallback={<SuspenseScreen />}>{children}</Suspense>
      </ErrorBoundary>
    </RelayEnvironmentProvider>
  );
}

function ProviderWrapper(): ReactElement {
  ...
  return (
    <RootProvider>
      // RelayEnvironmentWrapper wrapped App component
      <RelayEnvironmentWrapper>
        <App />
      </RelayEnvironmentWrapper>
    </RootProvider>
  );
}

export default ProviderWrapper;
// HeaderRightWidget.tsx
const HeaderRightWidget: FC = () => {
  const {
    state: { user },
    resetUser,
  } = useAuthContext();

  const handleSignOut = (): void => {
    // It'll be logout when executed resetUser of AuthContext.
    resetUser();
  };

  return (
    <HeaderRightContainer>
      <Avatar photoURL={user?.photoURL} onPress={handleSignOut} />
      <StyledText>{user?.name || 'no-name'}</StyledText>
    </HeaderRightContainer>
  );
};

I wanna find the best way for managing relay environment when users logging out/on and share it with your opinion.

If you can share a good way, please leave me a comment. Thanks for reading.

stale[bot] commented 3 years ago

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions.

matt-dalton commented 2 years ago

Did you find a good way of doing this @devethan?