Reactjs 响应本机将状态内的函数状态更改为状态内的类状态

Reactjs 响应本机将状态内的函数状态更改为状态内的类状态,reactjs,react-native,state,Reactjs,React Native,State,如何将此函数状态写入类状态?一般来说,我知道如何改变它,但这里让我很困惑的是,这个第三个常量状态在它自己内部使用了其他状态,state const scrollAnim在第三个状态内部使用。所以我不明白如何为类组件编写这个,因为它是在函数内部编写的 const [scrollAnim] = useState(new Animated.Value(0)); const [offsetAnim] = useState(new Animated.Value(0)); const [clamped

如何将此函数状态写入类状态?一般来说,我知道如何改变它,但这里让我很困惑的是,这个第三个常量状态在它自己内部使用了其他状态,state const scrollAnim在第三个状态内部使用。所以我不明白如何为类组件编写这个,因为它是在函数内部编写的

const [scrollAnim] = useState(new Animated.Value(0));
const [offsetAnim] = useState(new Animated.Value(0));

  const [clampedScroll, setClampedScroll] = useState(Animated.diffClamp(
    Animated.add(
      scrollAnim.interpolate({
        inputRange: [0, 1],
        outputRange: [0, 1],
        extrapolateLeft: 'clamp'
      }),
      offsetAnim
    ), 0, 1
  ));
我尝试的是:

state = {
    scrollAnim: (new Animated.Value(0)),
    offsetAnim: (new Animated.Value(0)),
    
    clampedScroll: (Animated.diffClamp(
      Animated.add(
        scrollAnim.interpolate({
          inputRange: [0, 1],
          outputRange: [0, 1],
          extrapolateLeft: 'clamp'
        }),
        offsetAnim
      ), 0, 1
    ))
  };

这很简单使用
this.state

state = {
    scrollAnim: (new Animated.Value(0)),
    offsetAnim: (new Animated.Value(0)),
    
    clampedScroll: (Animated.diffClamp(
      Animated.add(
        this.state.scrollAnim.interpolate({
          inputRange: [0, 1],
          outputRange: [0, 1],
          extrapolateLeft: 'clamp'
        }),
        this.state.offsetAnim
      ), 0, 1
    ))
  };

这很简单使用
this.state

state = {
    scrollAnim: (new Animated.Value(0)),
    offsetAnim: (new Animated.Value(0)),
    
    clampedScroll: (Animated.diffClamp(
      Animated.add(
        this.state.scrollAnim.interpolate({
          inputRange: [0, 1],
          outputRange: [0, 1],
          extrapolateLeft: 'clamp'
        }),
        this.state.offsetAnim
      ), 0, 1
    ))
  };

谢谢你,成功了!也请你知道什么是最好的方式来使用这个
const navbarTranslate=this.state.clampedScroll.interpolate({inputRange:[0,HEADER\u HEIGHT],outputRange:[0,-HEADER\u HEIGHT],interpolate:“clamp”,})类内组件?我需要更改什么?这里的状态变量是什么?另外,我建议您了解类组件中的状态,这样您就不需要任何帮助了,只是const varuiable,我在问如何在类中使用它。所以只需关注const部分plsahh是的,所以你要么将它标记为类外的const,要么将它标记为类属性,就像这样``constructor(props){this.navbarTranslate=…}``关于你的原始代码,我使用了它,当我在调用这些状态的地方使用它时,我不知道为什么。它表示this.state.scrollAnim未定义
this.state.clampedScroll(Animated.diffClamp(Animated.add)(this.state.scrollAnim.interpolate({inputRange:[0,1],outputRange:[0,1],extractionEleft:“clamp”,}),this.state.offsetAnim),0,height)谢谢你,成功了!也请你知道什么是最好的方式来使用这个
const navbarTranslate=this.state.clampedScroll.interpolate({inputRange:[0,HEADER\u HEIGHT],outputRange:[0,-HEADER\u HEIGHT],interpolate:“clamp”,})类内组件?我需要更改什么?这里的状态变量是什么?另外,我建议您了解类组件中的状态,这样您就不需要任何帮助了,只是const varuiable,我在问如何在类中使用它。所以只需关注const部分plsahh是的,所以你要么将它标记为类外的const,要么将它标记为类属性,就像这样``constructor(props){this.navbarTranslate=…}``关于你的原始代码,我使用了它,当我在调用这些状态的地方使用它时,我不知道为什么。它表示this.state.scrollAnim未定义
this.state.clampedScroll(Animated.diffClamp(Animated.add)(this.state.scrollAnim.interpolate({inputRange:[0,1],outputRange:[0,1],extractionEleft:“clamp”,}),this.state.offsetAnim),0,height)