Reputation: 1814
I'm using react-navigation in my react-native project in order to handle my navigation and routes.
In my case, i have a ViewA
and a ViewB
.
The ViewA
needs informations that i'm filling in my ViewB
.
The userflow is ViewA
-> ViewB
-> ViewA
.
** VIEW A **
import React from "react";
import { Button, Text, View } from "react-native";
class ViewA extends React.Component {
state = { selected: false };
onSelect = data => {
this.setState(data);
};
onPress = () => {
this.props.navigate("ViewB", { onSelect: this.onSelect });
};
render() {
return (
<View>
<Text>{this.state.selected ? "Selected" : "Not Selected"}</Text>
<Button title="Next" onPress={this.onPress} />
</View>
);
}
}
.
**VIEW B**
import React from "react";
import { Button } from "react-native";
class ViewB extends React.Component {
goBack() {
const { navigation } = this.props;
navigation.goBack();
navigation.state.params.onSelect({ selected: true });
}
render() {
return <Button title="back" onPress={this.goBack} />;
}
}
What i'm trying to do is : Instead of opening my ViewB
from left to right, i'd like it to be opened from bottom to up ('above' the ViewA) and having a close transition 'top-bottom'. Such as a Modal.
My issue is, i want to keep my StackNavigator as it is. And would like to custom this transition.
I don't want a Modal.
Thanks for your time and your help
Upvotes: 3
Views: 3222
Reputation: 538
Make sure you import this at the top of your page
import CardStackStyleInterpolator from "react-navigation/src/views/CardStack/CardStackStyleInterpolator";
This is what your navigator should look like for simple transitions.
StackNavigator(
{
Scenes...
},
{
transitionConfig: () => ({
screenInterpolator: props => {
// Basically you need to create a condition for individual scenes
if (props.scene.route.routeName === 'NameOfOneScene') {
// forVertical makes the scene transition for Top to Bottom
return CardStackStyleInterpolator.forVertical(props);
}
const last = props.scenes[props.scenes.length - 1];
// This controls the transition when navigation back toa specific scene
if (last.route.routeName === 'NameOfOneScene') {
// Here, forVertical flows from Top to Bottom
return CardStackStyleInterpolator.forVertical(props);
}
This declares the default transition for every other scene
return CardStackStyleInterpolator.forHorizontal(props);
},
navigationOptions: {
...
},
cardStyle: {
...
}
}
Upvotes: 4