Skip to content

Instantly share code, notes, and snippets.

@broerjuang
Created October 16, 2018 12:36
Show Gist options
  • Save broerjuang/5b265750d8993a0716b849b629aa42eb to your computer and use it in GitHub Desktop.
Save broerjuang/5b265750d8993a0716b849b629aa42eb to your computer and use it in GitHub Desktop.
Experimentation with Navigation
module Navigation = {
type actions = {pop: unit => unit};
[@bs.deriving abstract]
type navigation;
[@bs.send]
external navigate:
(navigation, ~routeName: string, ~params: Js.t({.})=?, unit) => unit =
"navigate";
};
module Abstraction = {
module type Config = {
type route;
let routeToJs: route => string;
};
module CreateNavigationAction = (C: Config) => {
[@bs.deriving abstract]
type jsProps = {navigation: Navigation.navigation};
let navigateTo = (~navigation, ~to_: C.route, ()) =>
navigation->Navigation.navigate(C.routeToJs(to_), ());
};
};
module Impl =
Abstraction.CreateNavigationAction({
[@bs.deriving jsConverter]
type route = [ | `First | `Second];
});
@broerjuang
Copy link
Author

First Screen

include Navigation.Impl;

module Styles = {
  open BsReactNative.Style;
  let container =
    style([flex(1.), justifyContent(Center), alignItems(Center)]);
};

let component = ReasonReact.statelessComponent("App");

let make = (~navigation, _children) => {
  ...component,
  render: _self =>
    BsReactNative.(
      <View style=Styles.container>
        <TouchableOpacity onPress={navigateTo(~navigation, ~to_=`Second)}>
          <Text value="Go to second route" />
        </TouchableOpacity>
      </View>
    ),
};

let default =
  ReasonReact.wrapReasonForJs(~component, jsProps =>
    make(~navigation=jsProps->navigationGet, [||])
  );

@broerjuang
Copy link
Author

Second Screen

include Navigation.Impl;

let component = ReasonReact.statelessComponent("FirstRoute");

let make = (~navigation, _children) => {
  ...component,
  render: _self =>
    BsReactNative.(
      <View>
        <TouchableOpacity onPress={navigateTo(~navigation, ~to_=`First)}>
          <Text value="Go to first route" />
        </TouchableOpacity>
      </View>
    ),
};

let default =
  ReasonReact.wrapReasonForJs(~component, jsProps =>
    make(~navigation=jsProps->navigationGet, [||])
  );

@broerjuang
Copy link
Author

broerjuang commented Oct 16, 2018

routes.js

// @flow

import {createStackNavigator} from 'react-navigation';
import App from './lib/js/src/First.bs';
import Second from './lib/js/src/Second.bs';

export default createStackNavigator({
  First: App,
  Second: Second,
});

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment