简体   繁体   English

使用嵌套的反应导航来反应本机HOC

[英]React Native HOC with nested react-navigation

I have a HOC used to pass react's Context as props to any component (a similar functionality and structure to redux): 我有一个HOC,用于将react的Context作为props传递给任何组件(与redux相似的功能和结构):

import React from 'react';

import { GlobalContext } from './context';

export const globalContext = (mapStateToProps = state => ({ ...state })) => Children => props => (
  <SignUpContext.Consumer>
    {state => (<Children {...mapStateToProps(state)} {...props} />)}
  </SignUpContext.Consumer>
);

This works as expected in normal components but when I try to use with a nested react navigator an error is thrown because cant access to the static router. 这在正常组件中按预期工作,但是当我尝试与嵌套的React导航器一起使用时,由于无法访问静态路由器而引发错误。

If I use either navigation or HOC It works but I can't manage to make both of them work 如果我使用导航或HOC都可以,但是我无法使两者都起作用

This would be a theoretical example of HOC and navigation: 这将是HOC和导航的理论示例:

import globalContext from './context'
import Screen from './screen';
import Screen2 from './screen2';

const MainStack = createBottomTabNavigator({
    Screen: {screen: globalContext()(Screen)},
    Screen2,
});

export default class Main extends Component {
  static router = MainStack.router;
  render() {
    console.log(this.props.navigator); //does exist
    return (
      <View >
      { /*some component over all navigator*/}
      <MainStack navigator={this.props.navigator} />
      </View>
    )
  }
}

ok, that failed but while I was editing I realized that It was stupid to use the class so it worked just exporting the createBottomTabNavigator 好的,那失败了,但是当我进行编辑时,我意识到使用类是愚蠢的,因此只导出createBottomTabNavigator就可以了

Wrap each screen by HOC then pass it to navigation may be helped. 通过HOC包装每个屏幕,然后将其传递给导航可能会有所帮助。

 import globalContext from './context' import Screen from './screen'; import Screen2 from './screen2'; const MainStack = createBottomTabNavigator({ globalContext()(Screen), globalContext()(Screen2), }); export default MainStack; 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM