From d238e1ab6cf67c337bb7f111f86f752e15a112b5 Mon Sep 17 00:00:00 2001 From: Laurent Cozic Date: Wed, 10 May 2017 18:58:02 +0000 Subject: [PATCH] Handle navigation state with Redux --- ReactNativeClient/debug-log.bat | 1 + ReactNativeClient/src/root.js | 133 ++++++++------------------------ 2 files changed, 32 insertions(+), 102 deletions(-) create mode 100644 ReactNativeClient/debug-log.bat diff --git a/ReactNativeClient/debug-log.bat b/ReactNativeClient/debug-log.bat new file mode 100644 index 000000000..65fcefe2d --- /dev/null +++ b/ReactNativeClient/debug-log.bat @@ -0,0 +1 @@ +adb logcat *:S ReactNative:V ReactNativeJS:V \ No newline at end of file diff --git a/ReactNativeClient/src/root.js b/ReactNativeClient/src/root.js index a4596fd3f..0683dab0c 100644 --- a/ReactNativeClient/src/root.js +++ b/ReactNativeClient/src/root.js @@ -25,68 +25,27 @@ const reducer = (state = defaultState, action) => { switch (action.type) { - case 'SET_BUTTON_NAME': + case 'Navigation/NAVIGATE': + case 'Navigation/BACK': - var state = shallowcopy(state); - state.myButtonLabel = action.name; - return state; + const nextStateNav = AppNavigator.router.getStateForAction(action, state.nav); + if (!nextStateNav) return state; + let newState = Object.assign({}, state); + newState.nav = nextStateNav; + return newState; - case 'INC_COUNTER': + case 'VIEW_NOTE': - var state = shallowcopy(state); - state.counter++; - return state; - - case 'VIEW_NOTE': - - // let state = Object.assign({}, state); - // state.selectedNoteId = action.id; + // TODO return state; - // - // state.counter++; - // return state; - } return state; } -// const appReducer = combineReducers({ -// reducer: reducer, -// }); - let store = createStore(reducer); -class MyInput extends Component { - - render() { - return - } - -} - -const mapStateToInputProps = function(state) { - return { text: state.defaultText } -} - -const mapDispatchToInputProps = function(dispatch) { - return { - onChangeText(text) { - dispatch({ - type: 'SET_BUTTON_NAME', - name: text - }); - } - } -} - -const MyConnectionInput = connect( - mapStateToInputProps, - mapDispatchToInputProps -)(MyInput) - - class NotesScreen extends React.Component { static navigationOptions = { title: 'Notes', @@ -102,7 +61,6 @@ class NotesScreen extends React.Component { navigate('Note') } /> - ); } @@ -123,7 +81,6 @@ class NoteScreen extends React.Component { navigate('Notes') } /> - ); } @@ -147,69 +104,41 @@ class ProfileScreen extends React.Component { } } - const AppNavigator = StackNavigator({ Notes: {screen: NotesScreen}, Note: {screen: NoteScreen}, Profile: {screen: ProfileScreen}, }); +class AppComponent extends React.Component { + render() { + return ( + + ); + } +} + +defaultState.nav = AppNavigator.router.getStateForAction(AppNavigator.router.getActionForPathAndParams('Notes')); + +const mapStateToProps = (state) => { + return { + nav: state.nav + }; +}; + +const App = connect(mapStateToProps)(AppComponent); + class Root extends React.Component { render() { return ( - + ); } } - - - -// const AppNavigator = StackNavigator({ -// Main: {screen: MainScreen}, -// Profile: {screen: ProfileScreen}, -// }); - -// class AppComponent extends React.Component { -// render() { -// return ( -// -// ); -// } -// } - -// const navInitialState = AppNavigator.router.getStateForAction(AppNavigator.router.getActionForPathAndParams('Main')); - -// const navReducer = (state = navInitialState, action) => { -// const nextState = AppNavigator.router.getStateForAction(action, state); -// return nextState || state; -// }; - -// const appReducer = combineReducers({ -// nav: navReducer, -// }); - -// const mapStateToProps = (state) => ({ -// nav: state.nav -// }); - -// const App = connect(mapStateToProps)(AppComponent); - -// const store = createStore(appReducer); - -// class Root extends React.Component { -// render() { -// return ( -// -// -// -// ); -// } -// } - export { Root }; \ No newline at end of file