I just updated to respond to native 0.54.0, which also includes alpha 1 of reaction 16.3, naturally, I get a lot of warnings about the wear of componentWillMount and componentWillReceiveProps .
I have an animated route component that relied on componentWillReceiveProps , in essence it gets a new path, compares it with the previous one, if it puts old children, animates them, sets new children and animates them.
Here is the code in question:
componentWillReceiveProps(nextProps: Props) { if (nextProps.pathname !== this.props.pathname) { this.setState({ previousChildren: this.props.children, pointerEvents: false }, () => this.animate(0) ); } }
Now for questions regarding the transfer of this value to static getDerivedStateFromProps
1) I no longer have access to this.props , therefore there is no access to the previous path, I think I can keep these details in a state, is this the right approach now? This is like repeating data.
2) Since I do not have access to this , I cannot name my animation function that relies on state. How can I get around this?
3) I need to set the state first and then call the animation, since getDerivedStateFromProps sets the state by returning values, after that I can not do much, so is there a way to set the state and then execute the Callback?
4) the pathname bit is only used in componentWillReceiveProps right now if I put it into a state and never use this.state inside getDerivedStateFromProps (because I cannot) this.state.pathname errors are as defined, but have never been used. Is the best approach here to make it static?
My first instinct was to change it to componentDidUpdate , but we should not use setState inside it correctly? (this works in my scenario, though).
componentDidUpdate(prevProps: Props) { if (this.props.pathname !== prevProps.pathname) { this.setState({ previousChildren: prevProps.children, pointerEvents: false }, () => this.animate(0) ); } }
NOTE. . As far as I heard, I think there is a function that appears in the βpauseβ that will allow us to save views set to the unmount event of the component? I was unable to find a link to this, but it looks like what I could use for this animation
For everyone who is interested, this is a snippet for the full component in question
// @flow import React, { Component, type Node } from "react"; import { Animated } from "react-native"; type Props = { pathname: string, children: Node }; type State = { animation: Animated.Value, previousChildren: Node, pointerEvents: boolean }; class OnboardingRouteAnomation extends Component<Props, State> { state = { animation: new Animated.Value(1), previousChildren: null, pointerEvents: true }; componentWillReceiveProps(nextProps: Props) { if (nextProps.pathname !== this.props.pathname) { this.setState({ previousChildren: this.props.children, pointerEvents: false }, () => this.animate(0) ); } } animate = (value: 0 | 1) => { Animated.timing(this.state.animation, { toValue: value, duration: 150 }).start(() => this.animationLogic(value)); }; animationLogic = (value: 0 | 1) => { if (value === 0) { this.setState({ previousChildren: null, pointerEvents: true }, () => this.animate(1)); } }; render() { const { animation, previousChildren, pointerEvents } = this.state; const { children } = this.props; return ( <Animated.View pointerEvents={pointerEvents ? "auto" : "none"} style={{ alignItems: "center", opacity: animation.interpolate({ inputRange: [0, 1], outputRange: [0, 1] }), transform: [ { scale: animation.interpolate({ inputRange: [0, 1], outputRange: [0.94, 1] }) } ] }} > {previousChildren || children} </Animated.View> ); } } export default OnboardingRouteAnomation;