使用带有样式组件的动画(react-native)

时间:2017-01-02 10:37:55

标签: javascript reactjs animation react-native styled-components

我遇到错误说明(通过iOS测试):

  

无法读取属性' getScrollableNode'为null

尝试在Animated样式工具旁边使用react-native styled-components进行反应和反应原生。

以下是我创建的<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

1 个答案:

答案 0 :(得分:15)

此问题实际上与样式组件无关。相反,它是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到无状态组件。并stateless components cannot have refs