React ネイティブ アプリを開発し、React ルーター ネイティブ v4 を使用しています。ドキュメントで示唆されているように、アニメーション部分を開発しようとしています。最初に、すべてがアニメーションやトランジションなしで機能することを確認しました。
私は実装を繰り返しましたが、これは私が今得た限りです:
私のメインコンポーネントは以下をレンダリングします:
// app.js:render
<ConnectedRouter history={history}>
<App />
</ConnectedRouter>
私の routes.js は次をレンダリングします (親コンポーネントの前に子を更新しないように、Switch に渡された location プロパティに注意してください):
// routes.js render
<ViewTransition pathname={location.pathname}>
<Switch location={location}>
<Route exact path={uri.views.main} component={Dashboard} />
<Route path={uri.views.login} component={Login} />
<Route path={uri.views.register} component={Register} />
</Switch>
</ViewTransition>
そして、アニメーションを処理する ViewTransition は、今では古いビューと新しいビューをフェードイン/アウトするだけです:
// view-transition.js
@withRouter
export default class ViewTransition extends Component {
static propTypes = {
children: PropTypes.node,
location: PropTypes.object.isRequired,
};
state = {
prevChildren: null,
opacity: new Animated.Value(1)
};
componentWillUpdate(nextProps) {
if (nextProps.location !== this.props.location) {
this.setState({ prevChildren: this.props.children }, this.animateFadeIn);
}
}
animateFadeIn = () => {
Animated.timing(this.state.opacity, {
toValue: 0,
duration: 150
}).start(this.animateFadeOut);
};
animateFadeOut = () => {
this.setState({ prevChildren: null }, () => {
Animated.timing(this.state.opacity, {
toValue: 1,
duration: 400
}).start();
});
};
render() {
const { children } = this.props;
const { prevChildren, opacity } = this.state;
return (
<Animated.View
style={{
...StyleSheet.absoluteFillObject,
opacity,
position: "absolute"
}}
>
{prevChildren || children}
</Animated.View>
);
}
}
上記のコードは機能しており、古いビューがフェードアウトし、新しいビューがフェードインするのを見ることができますが、問題があります.フェードアウトし始めると、どういうわけかコンポーネントが再びマウントされ、アニメーションが始まる直前にまばたきが見られます.私のコードの何が問題なのかを知るために。