Skip to content

Instantly share code, notes, and snippets.

@karlhorky
Forked from bsansouci/Loop-react-motion.js
Created July 20, 2016 16:20
Show Gist options
  • Save karlhorky/cca5aaa0d2ca3ab690a2d49a6a749f37 to your computer and use it in GitHub Desktop.
Save karlhorky/cca5aaa0d2ca3ab690a2d49a6a749f37 to your computer and use it in GitHub Desktop.
Example Loop component React-Motion
let Loop = React.createClass({
getInitialState() {
return {
isMovingToEnd: true
};
},
endValue(currVals) {
let {endValueProp, isDone, startValue} = this.props;
let {isMovingToEnd} = this.state;
if(isDone(currVals, isMovingToEnd)) {
this.setState(() => {
return !isMovingToEnd;
});
}
return isMovingToEnd ? endValueProp(currVals) : startValue(currVals);
},
render() {
return (
<Spring endValue={this.endValue}>
{this.props.children}
</Spring>
);
}
});
// You could use it like this
let Test = React.createClass({
endValue() {
return [0, 0, 0, 0];
},
startValue() {
return [10, 10, 10, 10];
},
isDone(currVals, isMovingToEnd) {
return isMovingToEnd ?
currVals.every(v => v === 0) :
currVals.every(v => v === 10);
},
render() {
return (
<Loop
endValue={this.endValue}
isDone={this.isDone}
startValue={this.startValue}>
</Loop>
);
}
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment