Skip to content

Instantly share code, notes, and snippets.

@bsansouci
Last active March 21, 2017 09:09
Show Gist options
  • Save bsansouci/987d25aaeb79d7e4d6b4 to your computer and use it in GitHub Desktop.
Save bsansouci/987d25aaeb79d7e4d6b4 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