【问题标题】:Using Imperative API with functional component将命令式 API 与功能组件一起使用
【发布时间】:2020-09-10 15:47:51
【问题描述】:

有没有办法将 [Lottie View]https://github.com/react-native-community/lottie-react-native 上的命令式 API 与功能组件一起使用?

我正在使用 Lottie View 组件,如下所示。我想要一种简单的方法来调用 play() 和 stop() 方法。我希望有一种方法可以在不将此组件转换为类的情况下做到这一点。

const GameInterface: React.FunctionComponent<GameInterfaceProps> = () => {
    return (
        <LottieView
            source={require('./Data/data.json')}
        />
    );
};

谢谢!

【问题讨论】:

    标签: react-native lottie


    【解决方案1】:
     // TODO: add an explanation
    
    
     const GameInterface = (props: {lottieViewRef: (ref: any) => void}) => {
    
        return (
            <LottieView
                source={require('./Data/data.json')}
                ref={(ref) => props.lottieViewRef(ref)}
            />
        );
    };
    
    
    const YouApp = () => {
    
        const gameRef = useRef(null);      
    
        return (
          <View style={styles.myBeautifullStyle}>
             <GameInterface 
               lottieViewRef={(ref) => gameRef = ref} 
             />
             <Button 
               onClick={() => gameRef.play()}
             />    
          </View>
        );
    
    }
    

    【讨论】:

    • 这种方法似乎确实有效。我现在唯一的问题是似乎只有 gameRef.play() 可以工作。当我调用其他方法时,我得到“gameRef.pause 不是函数”。这会是我的实现问题还是 Lottie 的问题?
    • 更新:我使用的是旧版本的 Lottie。我更新了,我很高兴。非常感谢您的回答!
    猜你喜欢
    • 2020-12-13
    • 2017-01-16
    • 2020-07-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-04-04
    • 2020-10-03
    • 1970-01-01
    相关资源
    最近更新 更多