亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

反應原生 - '未定義不是對象'?

反應原生 - '未定義不是對象'?

慕神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 關注
  • 156 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號