在 React Navigation 中,可以通过将uriPrefix
prop 传递给顶级导航器,或者{containerOptions: {URIPrefix: PREFIX}}
作为第二个参数传递给内置导航器(例如StackNavigator
)来使用深度链接。
当 Redux 与 React Navigation 集成时,顶级导航器会传递一个navigation
prop。
但是,在 RN 应用程序上同时启用 Redux 和深度链接时。uriPrefix
和navigation
prop 都需要传递给顶级导航器,这会引发错误,
这个导航器有导航和容器属性,所以不清楚它是否应该拥有自己的状态。
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;
Redux 和深度链接(使用 React Navigation)如何集成到 RN 应用程序中?