[英]React native - 'undefined is not an object'?
Alright, going off this answer React native - "this.setState is not a function" trying to animate background color?好吧,离开这个答案React native - “this.setState is not a function”试图为背景颜色设置动画? Im just trying to loop fade the background color of a view in React Native.
我只是想在 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);
}, []);
With this, everything checks out except var bgColor = this.state.color
which creates error有了这个,一切检查除了
var bgColor = this.state.color
会产生错误
undefined is not an object evaluating..
undefined 不是 object 评估..
I dont understand why this is a problem since I set color to useState('#fff')
I want to use color
in my Stylesheet as backgroundColor
.我不明白为什么这是一个问题,因为我将颜色设置为
useState('#fff')
我想在样式表中使用color
作为backgroundColor
。
How can I set this properly?如何正确设置?
If your component is a function you shouldn't use this.state.
如果您的组件是 function,则不应使用
this.state.
, but you have to call directly to the state name. ,但您必须直接调用 state 名称。
In your code:在您的代码中:
var bgColor = color.interpolate({...})
instead of:代替:
var bgColor = this.state.color.interpolate({...})
Reading State
读 State
When we want to display the current count in a class, we read this.state.count:
当我们想在 class 中显示当前计数时,我们阅读此.state.count:
<p>You clicked {this.state.count} times</p>
In a function, we can use count directly:
在 function 中,我们可以直接使用 count:
<p>You clicked {count} times</p>
Here is an example don't create state for animated value instead use memo to initialise it once and update it using timing function这是一个示例,不要为动画值创建 state,而是使用备忘录对其进行一次初始化并使用时序 function 对其进行更新
snack: https://snack.expo.io/GwJtJUJA0零食: https://snack.expo.io/GwJtJUJA0
code:代码:
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>
);
}
In functional components, you do not access the component's state properties/variables using this.state.abc
, instead you just use the name of the state variable directly.在功能组件中,您无需使用 this.state.abc 访问组件的 state 属性/变量,而是直接使用
this.state.abc
变量的名称。 So what you should do is:所以你应该做的是:
var bgColor = color.interpolate({
inputRange: [0, 300],
outputRange: ['rgba(255, 0, 0, 1)', 'rgba(0, 255, 0, 1)']
});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.