Last active
March 21, 2017 09:09
-
-
Save bsansouci/987d25aaeb79d7e4d6b4 to your computer and use it in GitHub Desktop.
Example Loop component React-Motion
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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