Skip to content

Commit

Permalink
back handling on root screen
Browse files Browse the repository at this point in the history
  • Loading branch information
tomoima525 committed Dec 18, 2017
1 parent 0f5937e commit 39d7792
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ public void onCreate(@Nullable Bundle savedInstanceState) {
setSupportActionBar(toolbar);
Button searchChuck = findViewById(R.id.search_chuck);
searchChuck.setOnClickListener(v ->
startActivity(MyReactActivity.createIntent(this, "search")));
startActivity(MyReactActivity.createIntent(this, "Search")));

TextView result = findViewById(R.id.result);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,10 @@ protected void onCreate(Bundle savedInstanceState) {
setContentView(R.layout.activity_react_search);
reactRootView = findViewById(R.id.rootView);

// bundle file to pass React Native
Bundle bundle = new Bundle();
bundle.putString("initialScene", getIntent().getStringExtra(KEY_SCENE));

reactInstanceManager = ReactInstanceManager.builder()
.setApplication(getApplication())
.setBundleAssetName("index.android.bundle")
Expand All @@ -37,7 +41,7 @@ protected void onCreate(Bundle savedInstanceState) {
.setUseDeveloperSupport(BuildConfig.DEBUG)
.setInitialLifecycleState(LifecycleState.RESUMED)
.build();
reactRootView.startReactApplication(reactInstanceManager, "ChuckNorrisViewer", null);
reactRootView.startReactApplication(reactInstanceManager, "ChuckNorrisViewer", bundle);
}

@Override
Expand Down
17 changes: 17 additions & 0 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import React,{Component} from 'react';
import {Provider} from 'react-redux';
import {createStore, applyMiddleware} from 'redux';
import PropTypes from 'prop-types';
import {createEpicMiddleware} from 'redux-observable';
import { composeWithDevTools } from 'remote-redux-devtools';
import chuckNorris from './js/reducers';
Expand All @@ -22,6 +23,14 @@ const store = createStore(chuckNorris, composeEnhancers(applyMiddleware(epicMidd

export default class ChuckNorrisViewer extends Component {

// Called from App. Child View Component can get values from here.
getChildContext() {
const { initialScene, data } = this.props;
if (data && typeof (data) === 'object') {
return { data, initialScene };
}
return { initialScene }; // No data
}
render() {
return (
<Provider store={store}>
Expand All @@ -31,4 +40,12 @@ export default class ChuckNorrisViewer extends Component {
}
}

ChuckNorrisViewer.childContextTypes = {
data: PropTypes.oneOfType([
PropTypes.shape({ }),
PropTypes.string,
]),
initialScene: PropTypes.string,
};

AppRegistry.registerComponent('ChuckNorrisViewer', () => ChuckNorrisViewer);
12 changes: 8 additions & 4 deletions js/scenes/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,15 @@ const initialSceneProps = {
renderLeftButton: () => <BackButton backTitle='back' onPress={() => console.log("back")} />,
};

const defaultSceneProps = {
initial: false,
renderLeftButton: () => {null},
};

const Scenes = (props, context) => (
<RouterWithRedux>
<Scene key="root"
{...( true ? initialSceneProps : {} )}
renderLeftButton={() => <BackButton backTitle='back' onPress={() => console.log("back")} />}
// renderLeftButton={() =>
// <BackButton
// backTitle='back'
Expand All @@ -29,9 +34,8 @@ const Scenes = (props, context) => (
title={name}
component={component}
hideNavBar={false}
initial={false}
renderBackButton={() =>
<BackButton backTitle='back' onPress={() => Actions.pop() }/>}
renderBackButton={() => <BackButton backTitle='back' onPress={() => Actions.pop() }/>}
{...(context.initialScene === name ? initialSceneProps : defaultSceneProps)}
/>
))}
</Scene>
Expand Down

0 comments on commit 39d7792

Please sign in to comment.