Skip to content
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

rendering global concerns, such as toasts #2709

Closed
mschipperheyn opened this issue Oct 7, 2017 · 6 comments
Closed

rendering global concerns, such as toasts #2709

mschipperheyn opened this issue Oct 7, 2017 · 6 comments
Labels

Comments

@mschipperheyn
Copy link

There are some global concerns that apply irrespective of Scene, such as toasts and bottom navigation bars. I would like to be able to render these on each scene of a DrawerNavigator, but it cannot overlay the slide out menu.

How would I go about this? There doesn't seem an obvious and I don't want to modify render functions on each Screen. Ideally, I would want to basically wrap the Scene component, but that doesn't seem to be the idea. Any other ways to achieve this?

@mschipperheyn
Copy link
Author

mschipperheyn commented Oct 8, 2017

Ok, so the solution I tried is creating a SceneWrapper and applying it like so:

@connect(
    state => ({
        status: state.status,
    }),
    dispatch => ({
        statusActions: bindActionCreators(statusActionCreators, dispatch),
    }),
)
export default class SceneWrapper extends React.Component {
    static propTypes = {
        isHeader: PropTypes.bool,
    };

    static defaultProps = {
        isHeader: true,
    };

    getConnectionToast() {
        const { isHeader } = this.props;

        return <ConnectionToast isHeader={isHeader} />;
    }

    getUnreachableToast() {
        const { isHeader } = this.props;

        return <UnreachableToast isHeader={isHeader} />;
    }

    _onStatusHide() {
        this.props.statusActions.hideStatus();
    }

    getStatusToast() {
        const { status } = this.props;
        return <StatusToast {...status.toJS()} onHide={this._onStatusHide} />;
    }

    render() {
        const { navigation } = this.props;

        const childrenWithProps = React.Children.map(this.props.children, child =>
            React.cloneElement(child, {
                navigation,
            }),
        );

        return [
            childrenWithProps,
            this.getConnectionToast(),
            this.getUnreachableToast(),
            this.getStatusToast(),
        ];
    }
}

import SceneWrapper from 'modules/navigation/components/SceneWrapper';
import { DrawerNavigator, StackNavigator } from 'react-navigation';

const applySceneWrapper = scene => <SceneWrapper>{scene}</SceneWrapper>;

const loginRoutez = {
    Login: {
        path: 'login',
        getScreen: () => applySceneWrapper(require('screens/LoginScreen').default),
    },
    [...]
};

const ScreenStack = StackNavigator(
   loginRoutez,
  {
    initialRouteName: 'Login'
  }
);

const PrimaryNav = DrawerNavigator({
    DrawerStack: {
      screen: ScreenStack
    }
});

But I'm getting The getScreen defined for route 'Login didn't return a valid screen or navigator.

Any suggestions?

@kelset
Copy link

kelset commented Oct 8, 2017

Hi @mschipperheyn! It looks like this is a general question about react-navigation: I suggest next time you use Stack Overflow which is better suited for answering these types of questions. I also encourage you to join the Reactiflux community on Discord. There are React Native and React Navigation channels with helpful people who might be able to answer your question. I would also like to mention that there is a collection of helpful links and tutorials for React Navigation here which might be helpful for you.

That said, I also suggest you read the ongoing article series about how to do advanced navigation with a Drawer on Medium, I think it will help you.

If you believe that you are experiencing a bug or have a feature request, please open a new Issue following the dedicated template or submit a PR.

@kelset kelset closed this as completed Oct 8, 2017
@kelset kelset added the question label Oct 8, 2017
@mschipperheyn
Copy link
Author

Sure, but perhaps I should have phrased this as an issue:

The getScreen defined for route 'Login didn't return a valid screen or navigator. occurs because your not allowed to return functions on the getScreen key. That seems rather arbitrary to me.

@alextorn
Copy link

@mschipperheyn how did you solve this issue?

@rcorrie
Copy link

rcorrie commented May 31, 2018

I am also interested in this. Is there a recommended approach @kelset for rendering global concerns using react-navigation?

Edit: This answered my question #456

@react-navigation react-navigation locked and limited conversation to collaborators May 31, 2018
@brentvatne
Copy link
Member

this is a legitimate weak point in react-navigation at the moment, please discuss in react-navigation/rfcs#15

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Projects
None yet
Development

No branches or pull requests

5 participants