为了账号安全,请及时绑定邮箱和手机立即绑定

反应原生 - '未定义不是对象'?

反应原生 - '未定义不是对象'?

慕神8447489 2022-11-11 16:32:32
好吧,离开这个答案React native - “this.setState is not a function”试图为背景颜色设置动画?我只是想在 React Native 中循环淡化视图的背景颜色。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 and hooks  //To change state 'color' - setColor('#ff0000');  const colors = ["#fff", "#ff0000", "#00ff00", "#0000ff", "#0077ff"];  const [color, setColor] = useState("#fff");  const [backgroundColor, setBackgroundColor] = useState(new Animated.Value(0));  const [time, setTime] = useState(0);  //const t = colors[randNum(0, colors.length)];  //random num, exclusive  function randNum(min, max) {    return Math.floor(min + Math.random() * (max - min));  }  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(backgroundColor, {      toValue: 100,      duration: 5000    }).start();  }, [backgroundColor]);  var bgColor = this.state.color.interpolate({    inputRange: [0, 300],    outputRange: ["rgba(255, 0, 0, 1)", "rgba(0, 255, 0, 1)"]  });  useEffect(() => {    const interval = setInterval(() => {      //setTime(new Date().getMilliseconds());      setColor("#ff0000");    }, 36000);    return () => clearInterval(interval);  }, []);有了这个,一切都会检查出来,除了var bgColor = this.state.color会产生错误undefined 不是评估的对象..我不明白为什么这是一个问题,因为我将颜色设置为useState('#fff')我想color在我的样式表中使用为backgroundColor.如何正确设置?
查看完整描述

3 回答

?
ABOUTYOU

TA贡献1812条经验 获得超5个赞

如果你的组件是一个你不应该使用的函数this.state.,但你必须直接调用状态名称。

在您的代码中:

var bgColor = color.interpolate({...})

代替:

var bgColor = this.state.color.interpolate({...})

从反应文档

阅读状态

当我们想在一个类中显示当前计数时,我们读取 this.state.count:

<p>You clicked {this.state.count} times</p>

在函数中,我们可以直接使用count:

<p>You clicked {count} times</p>


查看完整回答
反对 回复 2022-11-11
?
凤凰求蛊

TA贡献1825条经验 获得超4个赞

这是一个示例,不要为动画值创建状态,而是使用备忘录对其进行一次初始化并使用计时功能对其进行更新


小吃:https ://snack.expo.io/GwJtJUJA0


代码:


export default function App() {

  const { value } = React.useMemo(

    () => ({

      value: new Animated.Value(0),

    }),

    []

  );


  React.useEffect(() => {

    Animated.loop(

      Animated.sequence([

        Animated.timing(value, {

          toValue: 1,

          duration: 1000,

        }),

        Animated.timing(value, {

          toValue: 0,

          duration: 1000,

        })

      ])

    ).start();

  }, []);


  const backgroundColor = value.interpolate({

    inputRange: [0, 1],

    outputRange: ['#0dff4d', '#ff390d'],

  });


  return (

    <View style={styles.container}>

      <Animated.View style={{ width: 200, height: 100, backgroundColor }} />

    </View>

  );

}


查看完整回答
反对 回复 2022-11-11
?
侃侃无极

TA贡献2051条经验 获得超10个赞

在功能组件中,您不使用 访问组件的状态属性/变量this.state.abc,而是直接使用状态变量的名称。所以你应该做的是:


    var bgColor = color.interpolate({

      inputRange: [0, 300],

      outputRange: ['rgba(255, 0, 0, 1)', 'rgba(0, 255, 0, 1)']

    });


查看完整回答
反对 回复 2022-11-11
  • 3 回答
  • 0 关注
  • 127 浏览
慕课专栏
更多

添加回答

举报

0/150
提交
取消
微信客服

购课补贴
联系客服咨询优惠详情

帮助反馈 APP下载

慕课网APP
您的移动学习伙伴

公众号

扫描二维码
关注慕课网微信公众号