【问题标题】:react-native-router-flux: Navbar buttons need to access scene component methodsreact-native-router-flux:导航栏按钮需要访问场景组件方法
【发布时间】:2016-10-24 04:47:36
【问题描述】:

我正在使用 renderRightButton api 将 rightButton 插入 react-native-router-flux。我遇到的问题是 rightButton 需要访问组件中的方法。

class App extends React.Component {
  render() {
    return(
      <Router>
          <Scene
              key="firstScene"
              component={FirstScene}
              title="First scene"
              rightTitle="Apply"
              renderRightButton={this.getRightButton}
          />
      </Router>
    )
  }

  getRightButton() {
    return(

      // someMethodOnFirstSceneComponent lives on the FirstScene component

      <View onPress{this.someMethodOnFirstSceneComponent}>
        <Text>
          Invoke Function from Scene Component
        </Text>
      </View>
    )
  }
};

一种选择是将当前处于FirstScene 状态的所有数据放入redux reducer,然后将someMethodOnFirstSceneComponent 放在FirstScene 组件上。但是,我试图将状态保留在组件中,而不是依赖于 reducer,因为根据我的经验,它让事情变得更容易推理。

【问题讨论】:

  • 您找到解决方案了吗?

标签: react-native


【解决方案1】:

我找到了一种方法来做到这一点。不是一个很好的解决方案,但它有效。我在组件构造函数中设置 onRight 方法

  constructor(props) {
    super(props);
    this.props.component.onRight = () => {
      console.warn("call from my onRight function")}
    }
  }

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2019-03-12
    • 1970-01-01
    • 2017-01-05
    • 1970-01-01
    • 1970-01-01
    • 2016-12-22
    • 2017-08-19
    • 1970-01-01
    相关资源
    最近更新 更多