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

migration to react-navigation v2.x (#3068) #3141

Merged
merged 20 commits into from
Aug 8, 2018
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 1 addition & 2 deletions .prettierrc
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,5 @@
"singleQuote": true,
"semi": true,
"bracketSpacing": true,
"trailingComma": "all",
"eslintIntegration": true
"trailingComma": "all"
}
10 changes: 3 additions & 7 deletions Example/Example.js
Original file line number Diff line number Diff line change
Expand Up @@ -35,12 +35,8 @@ const styles = StyleSheet.create({
},
});

const reducerCreate = params => {
const defaultReducer = new Reducer(params);
return (state, action) => {
console.log('ACTION:', action);
return defaultReducer(state, action);
};
const stateHandler = (prevState, newState, action) => {
console.log('ACTION:', action);
};

const getSceneStyle = () => ({
Expand All @@ -53,7 +49,7 @@ const getSceneStyle = () => ({
const prefix = Platform.OS === 'android' ? 'mychat://mychat/' : 'mychat://';

const Example = () => (
<Router createReducer={reducerCreate} getSceneStyle={getSceneStyle} uriPrefix={prefix}>
<Router onStateChange={stateHandler} getSceneStyle={getSceneStyle} uriPrefix={prefix}>
<Overlay key="overlay">
<Modal key="modal" hideNavBar transitionConfig={() => ({ screenInterpolator: CardStackStyleInterpolator.forFadeFromBottomAndroid })}>
<Lightbox key="lightbox">
Expand Down
5 changes: 0 additions & 5 deletions index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,10 @@ import { StyleProp, Image, ViewStyle, TextStyle, ImageStyle } from "react-native
export var Router: RouterStatic;
export type Router = RouterStatic;

// Reducer
export var Reducer: any;
export type Reducer = any;

// Router
interface RouterProps extends React.Props<Router> {
sceneStyle?: StyleProp<ViewStyle>;
backAndroidHandler?: Function;
createReducer?: Function;
wrapBy?: Function;
scenes?: any;
}
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "react-native-router-flux",
"version": "4.0.0-beta.32",
"version": "4.0.0-beta.33",
"description": "React Native Router using Flux architecture",
"repository": {
"type": "git",
Expand Down
92 changes: 0 additions & 92 deletions src/Reducer.js

This file was deleted.

9 changes: 6 additions & 3 deletions src/Router.js
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ class App extends React.Component {
}

const Router = ({
sceneStyle, scenes, uriPrefix, navigator, getSceneStyle, children, onDeepLink, wrapBy, ...props
sceneStyle, onStateChange, scenes, uriPrefix, navigator, getSceneStyle, children, onDeepLink, wrapBy, ...props
}) => {
const data = { ...props };
if (getSceneStyle) {
Expand All @@ -102,10 +102,13 @@ const Router = ({
data.cardStyle = sceneStyle;
}
const AppNavigator = scenes || navigator || navigationStore.create(children, data, wrapBy);
if (onStateChange) {
navigationStore.onStateChange = onStateChange;
}
return <App {...props} onDeepLink={onDeepLink} navigator={AppNavigator} uriPrefix={uriPrefix} />;
};
Router.propTypes = {
createReducer: PropTypes.func,
onStateChange: PropTypes.func,
scenes: PropTypes.func,
navigator: PropTypes.func,
wrapBy: PropTypes.func,
Expand All @@ -116,7 +119,7 @@ Router.propTypes = {
onDeepLink: PropTypes.func,
};
Router.defaultProps = {
createReducer: null,
onStateChange: null,
scenes: null,
navigator: null,
wrapBy: props => props,
Expand Down
3 changes: 1 addition & 2 deletions src/index.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import * as ActionConst from './ActionConst';
import Reducer from './Reducer';
import Router from './Router';
import Scene from './Scene';
import Actions from './navigationStore';
Expand All @@ -12,5 +11,5 @@ import Overlay from './Overlay';
import pathParser from './pathParser';

export {
ActionConst, Reducer, Router, Scene, Actions, Modal, Lightbox, Stack, Drawer, Tabs, Overlay, pathParser,
ActionConst, Router, Scene, Actions, Modal, Lightbox, Stack, Drawer, Tabs, Overlay, pathParser,
};
67 changes: 53 additions & 14 deletions src/navigationStore.js
Original file line number Diff line number Diff line change
Expand Up @@ -325,12 +325,7 @@ function createNavigationOptions(params) {
}

if (navTransparent) {
res.headerStyle = {
backgroundColor: 'transparent',
zIndex: 100,
borderBottomWidth: 0,
elevation: 1,
};
res.headerTransparent = true;
}

if (backToInitial) {
Expand Down Expand Up @@ -471,25 +466,69 @@ function uniteParams(routeName, params) {
return res;
}

const defaultSuccess = () => {};
const defaultFailure = () => {};

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

My bad here for removing it - they were not being used before.

Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No worries, you just fixed eslint errors. It was used by removed setState, now I re-enabled handlers.

class NavigationStore {
_navigator = null;

refs = {};

states = {};

get currentScene() {
return getActiveState(this.state);
}
currentScene;

get prevScene() {
return getActiveState(this.prevState);
}
prevScene;

onNavigationStateChange = (prevState, currentState, action) => {
currentParams;

onStateChange;

onNavigationStateChange = async (prevState, currentState, action) => {
this.state = currentState;
this.prevState = prevState;
this.currentParams = action.params;
const activeState = getActiveState(this.state);
const currentScene = activeState.routeName;
this.currentParams = { ...activeState.params, ...action.params };
this.currentScene = currentScene;
this.prevScene = this.prevState ? getActiveState(this.prevState).routeName : null;
if (this.currentScene !== this.prevScene) {
// run onExit for old scene
if (this.prevScene) {
const exitHandler = this[this.prevScene + OnExit];
if (exitHandler) {
try {
const res = exitHandler(this.state);
if (res instanceof Promise) {
res.then(defaultSuccess, defaultFailure);
}
} catch (e) {
console.error('Error during onExit handler:', e);
}
}
}
if (this.states[currentScene]) {
const handler = this[currentScene + OnEnter];
const success = this.states[currentScene].success || defaultSuccess;
const failure = this.states[currentScene].failure || defaultFailure;
// call onEnter handler
if (handler) {
try {
const res = await handler(this.currentParams, this.state);
if (res) {
success(res);
} else {
failure();
}
} catch (e) {
failure({ error: e.message });
}
}
}
}
if (this.onStateChange) {
this.onStateChange(prevState, currentState, action);
}
};

setTopLevelNavigator = (navigatorRef) => {
Expand Down