首页 > 解决方案 > 将 Animated.Value 传递给反应原生的样式组件?

问题描述

如何将 Animated.Value 传递给样式化组件?

每当我取消注释bottom: ${p=>p.bottom};行时,我都会收到此错误:

NSString 类型的 JSON 值“[object Object]”无法转换为 YGValue。您是否忘记了 % 或 pt 后缀?

这是我的代码:

  ...
  const [value] = useState(new Animated.Value(-100));

  return (
    <StyledView
      as={Animated.View}
      bottom={value}
      {...props}>
      <Button
        title={'Show'}
        onPress={() => {
          Animated.timing(value, {toValue: 0, duration: 300}).start();
        }}
      />
      <Button
        title={'Hide'}
        onPress={() => {
          Animated.timing(value, {toValue: -100, duration: 300}).start();
        }}
      />
    </StyledView>
  );
  ...

const StyledView = styled.View`
    position: absolute;
    height: 200;
    bottom: ${p=>p.bottom};
    background-color: yellow;
    width: 100%;
`;

标签: react-nativestyled-componentsreact-animated

解决方案


推荐阅读