ReactNative

ReactNavigation初始化(一)

2019-06-20  本文已影响0人  Allens_Lee

一、安装包

  1. 安装react-navigation。
yarn add react-navigation
# or with npm
# npm install --save react-navigation
  1. 安装react-native-gesture-handler.
yarn add react-native-gesture-handler
# or with npm
# npm install --save react-native-gesture-handler
  1. 引入原生库到项目中。
react-native link react-native-gesture-handler

二、使用

  1. 初始化路由
const AppNavigator = createStackNavigator({
  Home: HomeScreen,
  Details: DetailsScreen,
}, {
    initialRouteName: 'Home',
});

export default createAppContainer(AppNavigator);

或者:

const RootStack = createStackNavigator(
    {
        Home: HomeScreen,
        Details: DetailsScreen,
    },
    {
        initialRouteName: 'Home',
    }
);

const AppContainer = createAppContainer(RootStack);

export default class App extends React.Component {
    render() {
        return <AppContainer />;
    }
}
  1. 在屏幕中移动
this.props.navigation.dispatch(StackActions.reset({
    index: 0,
    actions: [
        NavigationActions.navigate({ routeName: 'Details' })
    ],
}))
this.props.navigation.navigate('Details')

navigate功能大致意味着“转到此屏幕,如果要跳转的页面已经在屏幕上,它将不会有任何反应,如果要跳转的页面在之前的栈队列中,它将以pop的形式跳转回去。”

this.props.navigation.push("Details")

push代表都会向导航堆栈中添加新路径

this.props.navigation.pop(2, true)
<!-- 返回到栈顶 -->
this.props.navigation.popToTop()
this.props.navigation.goBack()
  1. 页面间参数的传递
/* 1. Navigate to the Details route with params */
this.props.navigation.navigate('Details', {
    itemId: 86,
    otherParam: 'anything you want here',
});
/* 2. Get the param, provide a fallback value if not available */
<!--  -->
const { navigation } = this.props;
<!-- 第一个参数为传递过来的参数的key,第二个参数为传递过来的参数的默认值 -->
const itemId = navigation.getParam('itemId', 'NO-ID');
const otherParam = navigation.getParam('otherParam', 'some default value');
  1. 配置标题栏
static navigationOptions = {
    title: 'Home',
  };
static navigationOptions = ({ navigation }) => {
    return {
      title: navigation.getParam('otherParam', 'A Nested Details Screen'),
    };
  };
this.props.navigation.setParams({ otherParam: 'Updated!' })}
static navigationOptions = {
    title: 'Home',
    headerStyle: {
        backgroundColor: '#f4511e',
    },
    headerTintColor: '#fff',
    headerTitleStyle: {
        <!-- 值为:100、200、300、400、500、600、700、800、900、normal、bold -->
        fontWeight: 'bold',     
    },
};
/* The header config from HomeScreen is now here */
defaultNavigationOptions: {
    headerStyle: {
    backgroundColor: '#f4511e',
    },
    headerTintColor: '#fff',
    headerTitleStyle: {
    fontWeight: 'bold',
    },
},

注意:在屏幕组件中定义的navigaitonOptions与其父堆栈导航的默认导航配置合并在一起时,会优先展示屏幕组件中定义的导航配置。

class LogoTitle extends React.Component {
    render() {
        return (
            <Image
                source={require('./assets/spiro.png')}
                style={{ width: 30, height: 30 }}
            />
        );
    }
}

使用:

static navigationOptions = {
    // headerTitle instead of title
    headerTitle: <LogoTitle />,
};
  1. 标题按钮
headerRight: (
    <Button
        title="Info"
        onPress={ () => Alert.alert("提示", "右边按钮")}
        color="#fff"
    />
)
static navigationOptions = ({ navigation }) => {
    return {
        headerTitle: <LogoTitle />,
        headerRight: (
        <Button
            onPress={navigation.getParam('increaseCount')}
            title="+1"
            color="#fff"
        />
        ),
    };
};

componentDidMount() {
    this.props.navigation.setParams({ increaseCount: this._increaseCount });
}

state = {
    count: 0,
};

_increaseCount = () => {
    this.setState({ count: this.state.count + 1 });
};
上一篇 下一篇

猜你喜欢

热点阅读