我按照React native - "this.setState is not a function" trying to animate background color?的建议合并了useEffect()钩子(Hook)后,才遇到此问题
随着以下,我得到

export default props => {
      let [fontsLoaded] = useFonts({
        'Inter-SemiBoldItalic': 'https://rsms.me/inter/font-files/Inter-SemiBoldItalic.otf?v=3.12',
            'SequelSans-RomanDisp' : require('./assets/fonts/SequelSans-RomanDisp.ttf'),
            'SequelSans-BoldDisp' : require('./assets/fonts/SequelSans-BoldDisp.ttf'),
            'SequelSans-BlackDisp' : require('./assets/fonts/SequelSans-BlackDisp.ttf'),
      });
      if (!fontsLoaded) {
        return <AppLoading />;
      } else {

    //Set states
      const [backgroundColor, setBackgroundColor] = useState(new Animated.Value(0));

      useEffect(() => {
        setBackgroundColor(new Animated.Value(0));
      }, []);    // this will be only called on initial mounting of component,
      // so you can change this as your requirement maybe move this in a function which will be called,
      // you can't directly call setState/useState in render otherwise it will go in a infinite loop.
      useEffect(() => {
        Animated.timing(this.state.backgroundColor, {
          toValue: 100,
          duration: 5000
        }).start();
      }, [backgroundColor]);

      var color = this.state.colorValue.interpolate({
        inputRange: [0, 300],
        outputRange: ['rgba(255, 0, 0, 1)', 'rgba(0, 255, 0, 1)']
      });

    const styles = StyleSheet.create({
      container: { flex: 1,
      alignItems: 'center',
      justifyContent: 'center',
      backgroundColor: color
    },
      textWrapper: {
        height: hp('70%'), // 70% of height device screen
        width: wp('80%'),   // 80% of width device screen
        backgroundColor: '#fff',
        justifyContent: 'center',
        alignItems: 'center',
      },
      myText: {
        fontSize: hp('2%'), // End result looks like the provided UI mockup
        fontFamily: 'SequelSans-BoldDisp'
      }
    });

      return (
        <Animated.View style={styles.container}>
          <View style={styles.textWrapper}>
            <Text style={styles.myText}>Login</Text>
          </View>
        </Animated.View>
      );
  }
};
我只是想制作动画以淡化 View 的背景色。我尝试删除第一个useEffect,以防造成一些冗余,但这没有任何作用。我是ReactNative的新手-这是怎么了?
编辑:
export default props => {
  let [fontsLoaded] = useFonts({
    'Inter-SemiBoldItalic': 'https://rsms.me/inter/font-files/Inter-SemiBoldItalic.otf?v=3.12',
        'SequelSans-RomanDisp' : require('./assets/fonts/SequelSans-RomanDisp.ttf'),
        'SequelSans-BoldDisp' : require('./assets/fonts/SequelSans-BoldDisp.ttf'),
        'SequelSans-BlackDisp' : require('./assets/fonts/SequelSans-BlackDisp.ttf'),
  });

  //Set states
    const [backgroundColor, setBackgroundColor] = useState(new Animated.Value(0));

    useEffect(() => {
      setBackgroundColor(new Animated.Value(0));
    }, []);    // this will be only called on initial mounting of component,
    // so you can change this as your requirement maybe move this in a function which will be called,
    // you can't directly call setState/useState in render otherwise it will go in a infinite loop.
    useEffect(() => {
      Animated.timing(useState(backgroundColor), {
        toValue: 100,
        duration: 7000
      }).start();
    }, [backgroundColor]);

    // var color = this.state.colorValue.interpolate({
    //   inputRange: [0, 300],
    //   outputRange: ['rgba(255, 0, 0, 1)', 'rgba(0, 255, 0, 1)']
    // });

//------------------------------------------------------------------->
  if (!fontsLoaded) {
    return <AppLoading />;
  } else {

    const styles = StyleSheet.create({
      container: { flex: 1,
      alignItems: 'center',
      justifyContent: 'center',
      backgroundColor: backgroundColor
新错误:

最佳答案

useFonts开始,在您返回<AppLoading />时,在您的第一个渲染中(仅在猜测中)只有!fontsLoaded钩子(Hook)会被调用。其余的钩子(Hook)位于else块中,这意味着在每个渲染器上钩子(Hook)的数量都不相同。
查看https://reactjs.org/docs/hooks-rules.html了解更多说明,尤其是https://reactjs.org/docs/hooks-rules.html#only-call-hooks-at-the-top-level

10-08 09:31