将动画与样式组件一起使用(反应本机)
Using Animated with styled-components (react-native)
我遇到以下错误(通过 iOS 测试):
Cannot read property 'getScrollableNode' of null
尝试使用 react-native 的 Animated along side styled-components 样式工具进行 react 和 react-native。
这是我创建的 <Logo />
组件的示例:
import React from 'react';
import { Image, Dimensions } from 'react-native';
import styled from 'styled-components/native';
const { width } = Dimensions.get('window');
const logoWidth = width - (width * 0.2);
const logoHeight = logoWidth * 0.4516;
const SLogoImage = styled(Image)`
width: ${logoWidth};
height: ${logoHeight};
`;
const Logo = ({ ...rest }) => (
<SLogoImage
source={require('../assets/logo.png')}
{...rest}
/>
);
export default Logo;
然后我将此组件导入到我想要对其应用动画的场景之一:
import React from 'react';
import { View, Animated } from 'react-native';
import Logo from '../components/Logo';
const ALogo = Animated.createAnimatedComponent(Logo);
class HomeScene extends Component {
state = {
fadeAnim: new Animated.Value(0)
}
componentDidMount() {
Animated.timing(
this.state.fadeAnim,
{ toValue: 1 }
).start()
}
render() {
<View>
<ALogo style={{ opacity: this.state.fadeAnim }} />
</View>
}
}
export default HomeScene;
这会导致上述错误,尝试使用谷歌搜索但无法找到任何解释。如有必要,请随时索取更多详细信息。
相关 GitHub 问题: https://github.com/styled-components/styled-components/issues/341
这个问题实际上与样式组件无关。相反,它是 react-native one
解决方法是使用 class
而不是无状态组件。
class Logo extends React.Component {
render () {
return (
<SLogoImage
source={require('./geofence.gif')}
{...this.props}
/>
)
}
}
这里是 github repo 它正在工作的地方。如果有人想重现它,只需取消注释 14-21 行即可看到错误。
我认为问题来自 Animated trying to attach ref
to a stateless component. And stateless components cannot have refs。
我遇到以下错误(通过 iOS 测试):
Cannot read property 'getScrollableNode' of null
尝试使用 react-native 的 Animated along side styled-components 样式工具进行 react 和 react-native。
这是我创建的 <Logo />
组件的示例:
import React from 'react';
import { Image, Dimensions } from 'react-native';
import styled from 'styled-components/native';
const { width } = Dimensions.get('window');
const logoWidth = width - (width * 0.2);
const logoHeight = logoWidth * 0.4516;
const SLogoImage = styled(Image)`
width: ${logoWidth};
height: ${logoHeight};
`;
const Logo = ({ ...rest }) => (
<SLogoImage
source={require('../assets/logo.png')}
{...rest}
/>
);
export default Logo;
然后我将此组件导入到我想要对其应用动画的场景之一:
import React from 'react';
import { View, Animated } from 'react-native';
import Logo from '../components/Logo';
const ALogo = Animated.createAnimatedComponent(Logo);
class HomeScene extends Component {
state = {
fadeAnim: new Animated.Value(0)
}
componentDidMount() {
Animated.timing(
this.state.fadeAnim,
{ toValue: 1 }
).start()
}
render() {
<View>
<ALogo style={{ opacity: this.state.fadeAnim }} />
</View>
}
}
export default HomeScene;
这会导致上述错误,尝试使用谷歌搜索但无法找到任何解释。如有必要,请随时索取更多详细信息。
相关 GitHub 问题: https://github.com/styled-components/styled-components/issues/341
这个问题实际上与样式组件无关。相反,它是 react-native one
解决方法是使用 class
而不是无状态组件。
class Logo extends React.Component {
render () {
return (
<SLogoImage
source={require('./geofence.gif')}
{...this.props}
/>
)
}
}
这里是 github repo 它正在工作的地方。如果有人想重现它,只需取消注释 14-21 行即可看到错误。
我认为问题来自 Animated trying to attach ref
to a stateless component. And stateless components cannot have refs。