Created
November 17, 2016 16:27
-
-
Save junkycoder/dee427abd936be2933d48ef386a1d08d to your computer and use it in GitHub Desktop.
React Native Router + goToLink
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import React, { Component } from 'react'; | |
import Router from './Router'; | |
import NewDocument from './NewDocument'; | |
import DocumentsList from './DocumentsList'; | |
export default class App extends Component { | |
state = { | |
documents: [], | |
newDocument: { | |
}, | |
}; | |
render() { | |
return ( | |
<Router> | |
<NewDocument | |
name="new" | |
title='Nový doklad' | |
document={this.state.newDocument} | |
/> | |
<DocumentsList | |
name="list" | |
title='Seznam dokladů' | |
documents={this.state.documents} | |
/> | |
</Router> | |
); | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import React, { Component, PropTypes } from 'react'; | |
import { | |
Text, | |
} from 'react-native'; | |
import { documentType } from '../types'; | |
const { | |
arrayOf, | |
} = PropTypes; | |
export default class DocumentsList extends Component { | |
static propTypes = { | |
documents: arrayOf(documentType) | |
} | |
render() { | |
return <Text style={{ padding: 50 }}>Seznam dokumentů</Text>; | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import React, { Component } from 'react'; | |
import { | |
Navigator, | |
} from 'react-native'; | |
const { NavigationBar } = Navigator; | |
import NavBar from './NavBar'; | |
export default class Router extends Component { | |
defaultRoute() { | |
const route = Object.create(this.props.children[0], {}); | |
// console.log(Object.isExtensible(route)); // === true | |
return route; | |
} | |
scene() { | |
return (route, navigator) => { | |
return React.cloneElement(route, { | |
goToLink: this.link(navigator) | |
}); | |
} | |
} | |
link(navigator) { | |
let routesByName = {}; | |
this.props.children.forEach(route => ( | |
routesByName[route.props.name] = route | |
)); | |
return routeName => navigator.replace( | |
Object.create(routesByName[routeName], {}) | |
); | |
} | |
render() { | |
const goToLink = navigator => this.link(navigator) | |
return ( | |
<Navigator | |
initialRoute={this.defaultRoute()} | |
renderScene={this.scene()} | |
navigationBar={<NavBar link={goToLink} />} | |
/> | |
); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment