react-navigation将props传递给类组件

时间:2017-05-29 08:41:15

标签: react-native react-native-navigation

假设我有以下代码段来创建DrawerNavigator

export const DrawerApp = DrawerNavigator({
    PageHome: {
        screen: InboxScreen
    },
},
{
    contentComponent: props => <RightMenuScreen />,
    drawerPosition: 'right'
});

我从示例here中读到,我可以使用类似

的语法将类似banner道具的道具传递给功能组件
const MyNavScreen = ({ navigation, banner }) => (<View><Text>{banner}</Text></View>);
// ...
// ...
// ...
const InboxScreen = ({ navigation }) => (
    <MyNavScreen banner={'InboxScreen'} navigation={navigation} />
);

但是,如果我使用类Component声明我的Component,如何归档将自定义道具传递给MyNavScreen

的相同行为
class InboxScreen extends Component {

    render() {
        // here I want to get a prop like `banner` or `callback` from props
    }
}

1 个答案:

答案 0 :(得分:0)

通过使用 this.props ,您可以实现相同的

class InboxScreen  extends Component{
static navigationOptions = {
  drawerLabel: 'Inbox',
  drawerIcon: ({ tintColor }) => (
    <MaterialIcons
      name="move-to-inbox"
      size={24}
      style={{ color: tintColor }}
    />
  ),
};
render (){
  return(
  <MyNavScreen banner={'Inbox Screen'} navigation={this.props.navigation} />
  );
}
}