* Update root package to react-native@0.49 and flow-bin@0.53
* Update NavigationPlayground package to react-native@0.49 and flow-bin@0.53
* Update ReduxExample package to react-native@0.49 and flow-bin@0.53
* flow-typed update (code-gen)
* Find-and-replace old types with new types
- `React.Element<*>` as the result of a render function is now `React.Node`
- `ReactClass<T>` as the type of a Component is now `React.ComponentType<typeof T>`
- `extends React.Component<DP, P, S>` is now `extends React.Component<P, S>`
- `React.Children<*>` as the type of a children prop is now `React.ChildrenArray<*>`
- Refs are now typed with `React.Ref`
There are also several tiny typo fixes in here.
* Avoid assuming state is NavigationRoute in addNavigationHelpers
For the root, top-level Navigator, the state is a simple `NavigationState` (with no route info) and we still use `addNavigationHelpers` for it. We can't assume `navigation.state.key` is accessible for this type, so we need to do runtime checks here.
* Fix type of NavigationScreenConfig
`navigationOptions` was incorrectly typed
* Give createNavigationContainer's State a type parameter
We want to be able to specifically type this guy so we can use specific NavigationState for things like the Drawer views without having to do runtime assertions about the state type
* Fix typing of NavigationAction
First of all, `NavigationAction` was being used as two different types, so I had to create the new `PossiblyDeprecatedNavigationAction` and add it as a type parameter everywhere. Second of all, generics and implied types were being used far more than they need to be, and they're hard to work with because Flow doesn't really explain things well when they go wrong.
* Make sure children scenes of CardStack have NavigationProp typed with NavigationRoute
CardStack's `NavigationScreenProp` is typed with `NavigationState`, but the children need `NavigationRoute`. Flow gets confused by the spread operator, even if the individual property is replaced later, so we need to explicitly list each property individually (just `dispatch`, actually).
* Allow prevTransitionProps parameter to be nullable
* Allow prevTransitionProps parameter to be nullable
* Narrow down a couple type parameters
* Move NavigationStateRoute cast to within Component.router check in createConfigGetter
* Update withCachedChildNavigation so it's typed correctly with React.ComponentType
* Add a $FlowFixMe for displayName
`React.StatelessFunctionalComponent` is missing `displayName` before Flow 0.54.0
See de37062ea2
* Make TouchableItem's onPress prop optional
`HeaderBackButton` can pass in an undefined onPress, and the React Native `Touchable*` components allow it too
* Simplify withCachedChildNavigation and get rid of generics
The generics don't help us here and they make the types more confusing
* Redefine NavigationComponent as direct intersection
The new type is technically identical to the old type, but Flow is confused by the old type...
* Save router to local var before checking createConfigGetter
Flow seems to think Component.router can change after the check
* Fix DrawerSidebar's navigation prop's type
In order for us to be able to generate a childNavigationProp, the original navigation prop has to have a NavigationState state.
* Colocate action.key check with action.type check in getStateForAction
For some reason, Flow thinks `action` can change sometime after `action.type` is checked. As a result, it's not properly concluding that `action.key` should exist. To avoid this, we pull `action.key` out immediately after doing the `action.type` check.
* Use bounded polymorphism to pass down all props in withCachedChildNavigation
Also makes sure the injected props are specified first in the union
Also removes an unused `eslint-disable-line` in `getScreenForRouteName`
* Make TabRouter's getComponentForState return NavigationComponent
`NavigationScreenComponent` is a subtype of `NavigationComponent`, but it's not clear that `getComponentForState` always returns a `NavigationScreenComponent`, as `getScreenForRouteName` returns `NavigationComponent`
* Define NavigationComponent's props using NavigationNavigatorProps
Right now the types ask Flow to infer the types of `NavigationComponent`'s props. (`NavigationComponent` is the union of `NavigationNavigator` and NavigationScreenComponent.) Instead, we type the props as subtypes of `NavigationNavigatorProps`, so we can properly access those props without type errors. We used bounded polymorphism in `createNavigationContainer` and `createNavigator` to pass down these props. We also explicitly type the action in `createNavigationContainer` so we can properly pass in a `NavigationInitAction` to the dispatch function.
* Parameterize NavigationTabRouterConfig
`NavigationTabRouterConfig` doesn't always take `NavigationTabScreenOptions`; sometimes, it takes `NavigationDrawerScreenOptions`.
* Make all NavigationOptions Exact types
We're seeing weird behavior when these are merged together. By making them Exact types, we make types don't get mangled when they're unioned.
https://flow.org/en/docs/types/unions/#toc-disjoint-unions-with-exact-types
* Silence errors in tests
These errors are unavoidable since we can't type the precise shape of a particular `NavigationState`.
* Include NavigationSetParamsAction in NavigationTabAction
`TabNavigator`'s `getStateForAction` definitely takes `NavigationSetParamsAction`.
* Explicitly list router as void in NavigationScreenComponent
This is so Flow knows that if a `NavigationComponent` has a `router` property, then it is definitely a `NavigationNavigator`.
* Fix website app navigator's type
* Fix createDocPage type in App
In an earlier commit I attempted to fix the types here, but I incorrectly read it as returning a navigator when it just returns a screen. For some reason the error didn't immediately show up.
* Fix NavigationComponent types
Five changes:
1. No longer parameterizing the props of `NavigationComponent`; directly specifying as `NavigationNavigatorProps`
2. Specify the two distinct kinds of route types (leaf and state) directly in `NavigationComponent`
3. Have `SceneView` directly take `NavigationComponent` and allow inferring of `NavigationSceneProp` type
4. Have `CardStack` directly take `NavigationComponent` as well
* Allow Flow to infer `NavigationAction` type in `createNavigator`
* Silence Flow in router code that handles navigator sub-routes
There's no way for us to tell Flow that routes that correspond to navigators should be `NavigationStateRoute`s
* Allow DrawerSidebar to take a null contentComponent
The way `DrawerNavigator` is set up, it's possible to pass in a null `contentComponent` in the config.
If somebody want to do this, we'll just make the `DrawerSidebar` not appear altogether. This is simpler than splitting the types so there's a possibly-unspecified one for the user input, but a definitely-specified one in `DrawerSidebar`.
* Specify missing screenProps prop in DrawerView
* Thread full NavigationStateRoute in to DrawerSidebar
This requiring making `withCachedChildNavigation` use bounded polymorphism to thread the `NavigationStateRoute` through instead of downgrading it to a `NavigationState`
* Make NavigationStateProp's state prop covariant
This means that `NavigationStateProp<NavigationStateRoute>` will be a subtype of `NavigationStateProp<NavigationRoute>` and `NavigationStateProp<NavigationState>`
* Silence Flow's complaints about a route not being a NavigationStateRoute
As mentioned in previous commits and in the comment, there is no way to type a specific navigation state shape, so we have to cast it and silence Flow's complaints.
* Get rid of style prop in CardStack
It looks like at some point, it was possible to specify a `style` prop that got passed all the way down to `Header`, the `TransitionConfig`'s `screenInterpolator`, and `Transitioner`. Doesn't look like we're using it anywhere, and there's a todo saying to remove it, and it's causing a type error.
* Infer type of options instead of using a subtype of {}
I think this has to do with {} allowing for unsealed objects. I'm not 100% sure, but this fixes 4/6 of the remaining errors!
* Fix two type errors in NavigationPlayground
One was just requiring an outdated type (`React.Children` instead of `React.ChildrenArray`), and the other was getting confused regarding types because of a spread operator.
* Use a covariant property to simplify withCachedChildNavigation's InputProps
This doesn't reduce any errors, but it's a bit simpler and cleaner.
* Silence last two Flow errors regarding withCachedChildNavigation injecting childNavigationProps
These are the only two errors I haven't been able to figure out at all. If I had more time I'd try and figure out the simplest case and report it to the Flow team, but I've been working on this for a week already and have to move on.
The issue at hand is that `withCachedChildNavigation` injects `childNavigationProps`, but Flow doesn't see this and thinks it needs to be specified by the view component. We're using the HOC pattern suggested by the Flow docs, and I've tried several other patterns to no avail.
* Use stock react-native instead of Expo's in NavigationPlayground
Expo's causes Flow errors, and apparently this is normal practice because the person who switched us to Expo did this too.
* Silence react-native-gesture-handler Flow errors in NavigationPlayground
Expo requires this package, but it causes Flow errors.
* Fix index check in DrawerSidebar
Silly me, falling for the typical "0 is falsey" problem...
* Rework deprecated action tests
In an earlier commit I moved the `NavigationActions.mapDeprecatedActionAndWarn` out from `getStateForAction` and into `createNavigationContainer`'s `dispatch` function. The tests need to be reworked to support this, as they were previously calling `getStateForAction` directly. I don't imagine any users are calling `getStateForAction` directly.
* Re-record two snapshots
The changes in these snapshots are by design and don't affect anything.
* Infer type of options in NavigationScreenConfig
The issue is that sometimes we include options for different navigators in a single blob.
* Infer types of action and options in createNavigationContainer
Particulary helps with Options. Makes the types more specific and gets rid of an error that only shows up when using the library
* Don't use generics for NavigationState in createNavigationContainer
There's no particular reason for having those generics there, since there's no way we'd be able to get Flow to understand the exact shape of particular navigation states anyways. This fixes a bug when integrating this version of the project into my repo.
* Move ReduxExample to react-native@0.49.3 instead of custom Expo version
7.4 KiB
Transitioner
Transitioner
is a React component that helps manage transitions for complex animated components. It manages the timing of animations and keeps track of various screens as they enter and leave, but it doesn't know what anything looks like, because rendering is entirely deferred to the developer.
Under the covers, Transitioner
is used to implement CardStack
, and hence the StackNavigator
.
The most useful thing Transitioner
does is to take in a prop of the current navigation state. When routes are removed from that navigation state, Transitioner
will coordinate the transition away from those routes, keeping them on screen even though they are gone from the navigation state.
Example
class MyNavView extends Component {
...
render() {
return (
<Transitioner
configureTransition={this._configureTransition}
navigation={this.props.navigation}
render={this._render}
onTransitionStart={this.onTransitionStart}
onTransitionEnd={this.onTransitionEnd}
/>
);
}
Props
configureTransition
function
Invoked on Transitioner.componentWillReceiveProps
, this function allows customization of animation parameters such as duration
. The value returned from this function will be fed into a timing function, by default Animated.timing()
, as its config.
Examples
_configureTransition(transitionProps, prevTransitionProps) {
return {
// duration in milliseconds, default: 250
duration: 500,
// An easing function from `Easing`, default: Easing.inOut(Easing.ease)
easing: Easing.bounce,
}
}
Note: duration
and easing
are only applicable when the timing function is Animated.timing
. We can also use a different timing function and its corresponding config parameters, like so:
_configureTransition(transitionProps, prevTransitionProps) {
return {
// A timing function, default: Animated.timing.
timing: Animated.spring,
// Some parameters relevant to Animated.spring
friction: 1,
tension: 0.5,
}
}
Flow definition
configureTransition: (
transitionProps: NavigationTransitionProps,
prevTransitionProps: ?NavigationTransitionProps,
) => NavigationTransitionSpec,
Parameters
transitionProps
: the current NavigationTransitionProps created from the current navigation state and propsprevTransitionProps
: the previous NavigationTransitionProps created from the previous navigation state and props
Returns
- An object of type NavigationTransitionSpec that will be fed into an Animated timing function as its config
navigation
prop
An object with state
that represents the navigation state, with routes
and an active route index
. Also includes dispatch
and other methods for requesting actions.
Example value
{
// Index refers to the active child route in the routes array.
index: 1,
routes: [
{ key: 'DF2FGWGAS-12', routeName: 'ContactHome' },
{ key: 'DF2FGWGAS-13', routeName: 'ContactDetail', params: { personId: 123 } }
]
}
Flow definition
export type NavigationState = {
index: number,
routes: Array<NavigationRoute>,
};
For more information about the NavigationRoute
type, check out its flow definition.
render
function
Invoked from Transitioner.render()
. This function performs the actual rendering delegated from Transitioner
. In this function, we can use the information included in the transitionProps
and prevTransitionProps
parameters to render scenes, create animations and handle gestures.
There are a few important properties of the transitionProps
and prevTransitionProps
parameters that are useful for the tasks mentioned above:
scenes: Array<NavigationScene>
- a list of all available scenesposition: NavigationAnimatedValue
- the progressive index of the transitioner's navigation stateprogress: NavigationAnimatedValue
- the value that represents the progress of the transition when navigation state changes from one to another. Its numeric value will range from 0 to 1.
For the complete list of properties of NavigationTransitionProps
, check out its flow definition.
Examples
transitionProps.scenes
is the list of all available scenes. It is up to the implementor to determine how to lay them out on the screen. For example, we can render the scenes as a stack of cards like so:
_render(transitionProps, prevTransitionProps) {
const scenes = transitionProps.scenes.map(scene => this._renderScene(transitionProps, scene));
return (
<View style={styles.stack}>
{scenes}
</View>
);
}
We can then use an Animated.View
to animate the transition. To create necessary animated style properties, such as opacity
, we can interpolate on position
and progress
values that come with transitionProps
:
_renderScene(transitionProps, scene) {
const { position } = transitionProps;
const { index } = scene;
const opacity = position.interpolate({
inputRange: [index-1, index, index+1],
outputRange: [0, 1, 0],
});
// The prop `router` is populated when we call `createNavigator`.
const Scene = this.props.router.getComponent(scene.route.routeName);
return (
<Animated.View style={{ opacity }}>
{ Scene }
</Animated.View>
)
}
The above code creates a cross fade animation during transition.
For a comprehensive tutorial on how to create custom transitions, see this blog post.
Flow definition
render: (transitionProps: NavigationTransitionProps, prevTransitionProps: ?NavigationTransitionProps) => React.Node,
Parameters
transitionProps
: the current NavigationTransitionProps created from the current state and propsprevTransitionProps
: the previous NavigationTransitionProps created from the previous state and props
Returns
- A ReactElement, which will be used to render the Transitioner component
onTransitionStart
function
Invoked when the transition animation is about to start.
Flow definition
onTransitionStart: (transitionProps: NavigationTransitionProps, prevTransitionProps: ?NavigationTransitionProps) => void,
Parameters
transitionProps
: the current NavigationTransitionProps created from the current state and propsprevTransitionProps
: the previous NavigationTransitionProps created from the previous state and props
Returns
- none.
onTransitionEnd
function
Invoked once the transition animation completes.
Flow definition
onTransitionEnd: () => void
Parameters
- none.
Returns
- none.