Created
September 19, 2018 23:02
-
-
Save necolas/f9034091723f1b279be86c7429eb0c96 to your computer and use it in GitHub Desktop.
Next.js links with React Native for Web
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 NextLink from 'next/link'; | |
import React from 'react'; | |
import { Text } from 'react-native-web'; | |
// https://github.com/zeit/next.js#with-link | |
// Combines the Next.js <Link> with React Native's <Text> component. | |
// Enables use like this: | |
// | |
// <Link | |
// href={href} | |
// prefetch | |
// style={styles.enhance} | |
// > | |
// Link text | |
// </Link> | |
// | |
export default Link extends React.Component { | |
static propTypes = { | |
...NextLink.propTypes, | |
...Text.propTypes | |
} | |
blur() { | |
this._textRef.blur(); | |
} | |
focus() { | |
this._textRef.focus(); | |
} | |
setNativeProps(props) { | |
this._textRef.setNativeProps(props) | |
} | |
_setRef = (c) => { | |
this._textRef = c; | |
} | |
render() { | |
const { | |
as, | |
passHref, // ignore | |
scroll, | |
shallow, | |
replace, | |
...rest | |
} = this.props; | |
return ( | |
<NextLink | |
href={href} | |
passHref={true} | |
prefetch={prefetch} | |
replace={replace} | |
scroll={scroll} | |
shallow={shallow} | |
> | |
<Text | |
accessibilityRole='link' | |
ref={this._setRef} | |
{...rest} | |
/> | |
</NextLink> | |
) | |
} | |
} |
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 from 'react'; | |
import Router from 'next/router'; | |
import { Text } from 'react-native-web'; | |
// https://github.com/zeit/next.js#imperatively | |
export default Link extends React.Component { | |
static propTypes = Text.propTypes; | |
blur() { | |
this._textRef.blur(); | |
} | |
focus() { | |
this._textRef.focus(); | |
} | |
setNativeProps(props) { | |
this._textRef.setNativeProps(props) | |
} | |
_handlePress = () => { | |
Router.push(this.props.href); | |
} | |
_setRef = (c) => { | |
this._textRef = c; | |
} | |
render() { | |
return ( | |
<Text | |
accessibilityRole='link' | |
onPress={this._handlePress} | |
ref={this._setRef} | |
{...rest} | |
/> | |
) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
@daveteu It depends on what you want your end product to be. You can use map
react-router/native
andreact-router
to a platform file. This would make it really easy to manage routing for ios/android/web. It would look something like this:and
Then use it like so:
From what I remember this doesn't give you a stack navigator when you compile the native app though. If you did want that experience then you could have two entries points that also have the respective navigators. More maintenance but IMO better experience. This would then give you the option to use the next/link, next/router etc in the web entry point.