In React Navigation, deep linking can be utilized by passing uriPrefix
prop to the top level navigator, or by passing {containerOptions: {URIPrefix: PREFIX}}
as a second parameter to a built-in navigator (e.g. StackNavigator
).
When Redux is integrated with React Navigation, the top level navigator is passed a navigation
prop.
But, when enabling both Redux and and deep linking on a RN app. uriPrefix
and navigation
prop both need to be passed to a top level navigator, which throws an error,
This navigator has both navigation and container props, so it is unclear if it should own its own state.
const S1 = () => <View><Text>S1 text</Text></View>;
const S2 = () => <View><Text>S2 text</Text></View>;
const S3 = () => <View><Text>S3 text</Text></View>;
const AppNav = StackNavigator(
{
S1: {screen: S1, path: 's1'},
S2: {screen: S2, path: 's2'},
S3: {screen: S3, path: 's3'}
}
);
@connect(state => ({nav: state.nav}))
class App extends Component {
render() {
const
{ dispatch, nav } = this.props,
uriPrefix = Platform.OS == 'android' ? 'http://localhost/' : 'http://';
return (
<AppNav
navigation={addNavigationHelpers({dispatch: this.props.dispatch, state: this.props.nav})}
uriPrefix={uriPrefix}
/>
);
}
}
const navReducer = (state, action) => (AppNav.router.getStateForAction(action, state) || state);
const rootReducer = combineReducers({nav: navReducer});
const RootApp = props =>
<Provider store={createStore(rootReducer)}>
<App />
</Provider>;
export default RootApp;
How can Redux and deep linking (using React Navigation) both be integrated in a RN app?
Deep Linking is a technique in which a given URL or resource is used to open a specific page or screen on mobile. So, instead of just launching the app on mobile, a deep link can lead a user to a specific screen within the app, providing a better user experience.
Can I store the navigation state in Redux too? This is not possible. We don't support it because it's too easy to shoot yourself in the foot and slow down / break your app.
The Link component lets us navigate to a screen using a path instead of a screen name based on the linking options. It preserves the default behavior of anchor tags in the browser such as Right click -> Open link in new tab" , Ctrl+Click / ⌘+Click etc. It uses a Text component under the hood.
See @kristfal comment on Redux can not be used with deep linking #1189.
Or @grabbou comment.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With