将Animated.Value传递给React Native中的样式化组件?

时间:2020-04-20 11:43:29

标签: react-native styled-components react-animated

如何将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%;
`;

0 个答案:

没有答案