React Tween State Animation Library
react-tween-state is an animation utility for React applications that enables smooth interpolation of component state values over time. It provides a `this.tweenState` method, which functions similarly to `this.setState`, but initiates an animated transition of a state property to a target value over a specified duration. The library relies heavily on React's deprecated mixin system (`React.createClass`) for integration into components. The current and likely final stable version is 0.1.5, released in 2016, and the project has been unmaintained since then. Its core differentiators, such as direct state manipulation via mixins and an imperative `tweenState` API, are now considered anti-patterns in modern React development, which favors declarative component-based animation libraries and hooks. It does not support newer React features or functional components.
Common errors
-
TypeError: React.createClass is not a function
cause Attempting to use `React.createClass` (required by `react-tween-state`) with React v16 or newer, where this API has been removed.fixThis library is incompatible with modern React. You must downgrade your React version to v15 or earlier, or preferably, choose a modern animation library that uses class components or functional components/hooks. -
Invariant Violation: Mixins are deprecated and not supported in React 16+.
cause React versions from v0.13.0 onwards deprecated mixins, and v16 removed them entirely. Using `mixins: [tweenState.Mixin]` triggers this error.fixSee the fix for `TypeError: React.createClass is not a function`. The library cannot be used with modern React versions due to its reliance on deprecated features. -
TypeError: this.tweenState is not a function
cause The `tweenState` method is provided via the `tweenState.Mixin`. This error occurs if `tweenState.Mixin` is not included in the component's `mixins` array, or if the component is not created using `React.createClass`.fixEnsure your component is defined using `React.createClass` and includes `tweenState.Mixin` in its `mixins` array. Example: `var App = React.createClass({ mixins: [tweenState.Mixin], /* ... */ });` -
Uncaught ReferenceError: require is not defined
cause Using `require()` syntax directly in a browser environment without a CommonJS bundler (like Webpack or Browserify) in place, or without a global `require` implementation.fixThis library is designed for CommonJS environments. For browser usage, you need to use a module bundler to process the `require()` calls or ensure the library is included via a script tag in a way that exposes `tweenState` globally, though a bundler is the standard approach.
Warnings
- breaking This library is built exclusively for older versions of React (pre-v0.13 through v15) and relies on `React.createClass` and the mixin system. It is fundamentally incompatible with React v16 and newer, which removed `React.createClass` and deprecated mixins.
- gotcha The library is unmaintained since 2016 (version 0.1.5) and receives no updates, bug fixes, or security patches. Using it in production introduces significant technical debt and potential vulnerabilities.
- gotcha `react-tween-state` does not offer official TypeScript definitions. While community types might exist, they are not guaranteed to be up-to-date or accurate, leading to potential type-related issues when used in TypeScript projects.
- gotcha The `this.tweenState` call immediately sets the underlying state field to its `endValue` and then interpolates a separate 'tweening value' accessible via `this.getTweeningValue`. This can be counter-intuitive if you expect the actual component state to reflect the interpolated value at all times.
Install
-
npm install react-tween-state -
yarn add react-tween-state -
pnpm add react-tween-state
Imports
- tweenState (main import)
import tweenState from 'react-tween-state'
const tweenState = require('react-tween-state'); - Mixin
class App extends React.Component { /* ... */ }var App = React.createClass({ mixins: [tweenState.Mixin], /* ... */ }); - easingTypes
import { easingTypes } from 'react-tween-state'tweenState.easingTypes.easeInOutQuad
Quickstart
const tweenState = require('react-tween-state');
const React = require('react');
const ReactDOM = require('react-dom'); // Required for rendering
const App = React.createClass({
mixins: [tweenState.Mixin],
getInitialState: function() {
return { left: 0 };
},
handleClick: function() {
// Animate 'left' property of the state
this.tweenState('left', {
easing: tweenState.easingTypes.easeInOutQuad,
duration: 500,
endValue: this.state.left === 0 ? 400 : 0,
onEnd: () => console.log('Animation ended!')
});
},
render: function() {
const style = {
position: 'absolute',
width: 50,
height: 50,
backgroundColor: 'lightblue',
left: this.getTweeningValue('left'), // Retrieve the current tweening value
cursor: 'pointer',
display: 'flex',
alignItems: 'center',
justifyContent: 'center',
color: 'white'
};
return (
<div style={style} onClick={this.handleClick}>
Click Me
</div>
);
}
});
// This code needs an HTML file with <div id="root"></div> and
// older versions of React/ReactDOM (e.g., v15) to run successfully.
// For example, in an HTML file with script tags for React, ReactDOM, and this example:
// ReactDOM.render(<App />, document.getElementById('root'));