entria / ReactNavigationRelayModern

React Navigation integration with Relay
173 stars 23 forks source link

static navigationOptions ignored at component level #24

Closed ssomnoremac closed 6 years ago

ssomnoremac commented 6 years ago

in other words, this doesn't work for me:

  static navigationOptions = {
    title: 'UserList',
  };

Was this ever working for anyone else? Works if set at navigator level.

sibelius commented 6 years ago

You can use this snippet to help you

All root component needs a QueryRenderer, so I made this:

// @flow
import * as React from 'react';
import hoistStatics from 'hoist-non-react-statics';
import ErrorView from '../components/common/Error';
import LoadingView from '../components/common/LoadingView';

import type { GraphQLTaggedNode, Variables } from 'react-relay';

import { QueryRenderer } from 'react-relay';

import environment from './environment';

type Config = {
  query: ?GraphQLTaggedNode,
  queriesParams?: ?(props: Object) => Object,
  variables?: Variables,
};

export default function createQueryRenderer(
  FragmentComponent: React.ComponentType<*>,
  Component: React.ComponentType<*>,
  config: Config,
): React.ComponentType<*> {
  const { query, queriesParams } = config;

  class QueryRendererWrapper extends React.Component<{}> {
    render() {
      const variables = queriesParams ? queriesParams(this.props) : config.variables;

      return (
        <QueryRenderer
          environment={environment}
          query={query}
          variables={variables}
          render={({ error, props, retry }) => {
            if (error) {
              return <ErrorView error={error} retry={retry} />;
            }

            if (props) {
              return <FragmentComponent {...this.props} query={props} />;
            }

            return <LoadingView />;
          }}
        />
      );
    }
  }

  return hoistStatics(QueryRendererWrapper, Component);
}

hoistStatics(QueryRendererWrapper, Component);

will copy the statics from your component to the QueryRenderer generated one

ssomnoremac commented 6 years ago

Amazing, thanks! I can submit a PR if you'd like.

sibelius commented 6 years ago

Send it please