无法访问子渲染上的导航

时间:2018-09-27 11:08:04

标签: react-native react-navigation

能否有人向我解释为什么我可以在主渲染而不是在子渲染上访问this.props.navigation,也请让我知道如何将此参数传递给其他渲染方法

class HomeScreen extends React.Component {

renderContent() {
  return (
    <View>    
        <Button
        title="ProfileScreen"
        onPress={() =>
        navigate('ProfileScreen')
        } />   
    </View>
  );
}

render() {
  const { navigate } = this.props.navigation;
  return (
        <ReactNativeParallaxHeader
          headerMinHeight={24}
          renderContent={this.renderContent}
        />
    );
  }
}

export default HomeScreen

我尝试实现箭头功能,但仍然没有任何反应,并且未定义

1 个答案:

答案 0 :(得分:1)

因为您要在render()方法中定义导航。

这应该有效

class HomeScreen extends React.Component {

let navigate;

constructor(props) {
    super(props)
    navigate = props.navigation.navigate;
}


renderContent() {
  return (
    <View>    
        <Button
        title="ProfileScreen"
        onPress={() =>
        navigate('ProfileScreen')
        } />   
    </View>
  );
}

render() {
  
  return (
        <ReactNativeParallaxHeader
          headerMinHeight={24}
          renderContent={this.renderContent}
        />
    );
  }
}

export default HomeScreen