【发布时间】:2020-11-01 12:28:25
【问题描述】:
好的,我尝试每 x 毫秒更新一次组件的属性和/或文本。我深入了解https://www.npmjs.com/package/react-interval-renderer 之类的,但是我遇到了与 s 有关的错误。
我现在只是查看react native (IOS). how to update value date (millisecond),但是我无法将其格式化为我的文件。
我有:
export default props => {
let [fontsLoaded] = useFonts({
'Inter-SemiBoldItalic': 'https://rsms.me/inter/font-files/Inter-SemiBoldItalic.otf?v=3.12',
});
this.state ={
color: "#fff",
colorAnimTime: 36000,
time: 0
}
setInterval(() => {
this.setState({
time: new Date().getMilliseconds()
});
}, 1000);
//------------------------------------------------------------------->
if (!fontsLoaded) {
return <AppLoading />;
} else {
const styles = StyleSheet.create({
container: { flex: 1,
alignItems: 'center',
justifyContent: 'center',
},
textWrapper: {
height: hp('70%'), // 70% of height device screen
width: wp('80%'), // 80% of width device screen
justifyContent: 'center',
alignItems: 'center',
},
myText: {
fontSize: hp('5.5%'), // End result looks like the provided UI mockup
fontFamily: 'SequelSans-BlackDisp'
}
});
return (
<AnimatedBackgroundColorView
color='#00acdd'
delay={0.5}
duration={36000}
initialColor={this.state.color}
style={styles.container}>
<View style={styles.textWrapper}>
<Text style={styles.myText}>COLOR</Text>
</View>
</AnimatedBackgroundColorView>
);
}
引用的答案使用componentDidMount() { 将setInterval 括起来,但是如果我把它放在我目前拥有setInterval 的地方会出现语法错误
现在我得到了
this.setState 不是函数
并尝试将setInterval 绑定到右侧this,但我相信我不明白如何设置我的文件。
1000 毫秒后,我想更改 <AnimatedBackgroundColorView> 的“颜色”并重新加载组件。 (所以它再次动画 - https://www.npmjs.com/package/react-native-animated-background-color-view)
我该怎么做?
【问题讨论】:
-
您是否尝试过为
time: new Date().getMilliseconds()使用挂钩?你能做类似const [time, setTime] = React.useState(null)setTime(new Date().getMilliseconds()) -
能否提供完整代码的答案?
-
这第一条评论有帮助吗?你试过了吗
-
@BARNOWL 我试过了,但我不确定我是否理解你,我收到警告“待处理的回调数量过多”
-
您收到过多的待处理回调,因为您创建了一个无限循环。
setTime,设置一个新的状态,强制重新渲染,再次调用setTime,等等。
标签: javascript reactjs react-native render