feat: add native container with back button integration (#48)

This commit is contained in:
Michał Osadnik
2019-08-14 15:04:21 +01:00
committed by Satyajit Sahoo
parent 4a3db4e6f4
commit b7735af7fc
12 changed files with 94 additions and 7 deletions

View File

@@ -99,8 +99,19 @@ const Container = React.forwardRef(function NavigationContainer(
listeners[0](navigation => navigation.dispatch(action));
};
const canGoBack = () => {
const { result, handled } = listeners[0](navigation =>
navigation.canGoBack()
);
if (handled) {
return result;
} else {
return false;
}
};
React.useImperativeHandle(ref, () => ({
dispatch,
...(Object.keys(BaseActions) as Array<keyof typeof BaseActions>).reduce<
any
>((acc, name) => {
@@ -114,6 +125,8 @@ const Container = React.forwardRef(function NavigationContainer(
);
return acc;
}, {}),
dispatch,
canGoBack,
}));
const navigationStateRef = React.useRef<State>();

View File

@@ -55,12 +55,13 @@ export default function useDescriptors<
getState,
setState,
addActionListener,
addFocusedListener,
onRouteFocus,
router,
emitter,
addFocusedListener,
}: Options<ScreenOptions>) {
const [options, setOptions] = React.useState<{ [key: string]: object }>({});
const context = React.useMemo(
() => ({
navigation,

View File

@@ -243,9 +243,9 @@ export default function useNavigationBuilder<
setState,
onRouteFocus,
addActionListener,
addFocusedListener,
router,
emitter,
addFocusedListener,
});
return {

View File

@@ -35,7 +35,7 @@ export default function useNavigationHelpers<
const { performTransaction } = React.useContext(NavigationStateContext);
return React.useMemo(() => {
const dispatch = (action: Action | ((state: State) => State)) => {
const dispatch = (action: Action | ((state: State) => State)) =>
performTransaction(() => {
if (typeof action === 'function') {
setState(action(getState()));
@@ -43,7 +43,6 @@ export default function useNavigationHelpers<
onAction(action);
}
});
};
const actions = {
...router.actionCreators,