Skip to content

Instantly share code, notes, and snippets.

@rt2zz
Last active March 23, 2016 04:07
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save rt2zz/80802051d6d49816985d to your computer and use it in GitHub Desktop.
Save rt2zz/80802051d6d49816985d to your computer and use it in GitHub Desktop.
animations
import React, {
Animated,
Component,
PropTypes,
} from 'react-native'
import _ from 'lodash'
import config from '../config'
class Animation extends Component {
_animatedValues = {};
static propTypes = {
rules: PropTypes.object.isRequired,
Inner: PropTypes.func.isRequired,
restProps: PropTypes.object,
};
componentWillMount() {
this._animatedValues = _.mapValues(this.props.rules, (mapProps) => new Animated.Value(mapProps(this.props.restProps)))
}
componentWillReceiveProps(nextProps) {
_.forEach(nextProps.rules, (mapProps, key) => {
let toValue = mapProps(nextProps.restProps)
// no animations in lowPerfMode
!config.lowPerfMode
? Animated.spring(this._animatedValues[key], {toValue}).start()
: this._animatedValues[key].setValue(toValue)
})
}
render() {
let {Inner, restProps} = this.props
return <Inner {...restProps} {...this._animatedValues} />
}
}
export default function animator(rules) {
return (Inner) => {
return (props) => <Animation restProps={props} rules={rules} Inner={Inner} />
}
}
let aniFromProps = (props) => !props.something // animate between 0 or 1 based on some boolean
export default animator({ani: aniFromProps})(SomeComponent)
@gnoff
Copy link

gnoff commented Mar 23, 2016

not sure i follow how rules are executed and sequenced but I think that is just because i've never actually used Animated in react native. I like the lowPerfMode flag. that's pretty cool

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment