gpt4 book ai didi

javascript - react 原生 - "Rendered more hooks than during the previous render?"

转载 作者:行者123 更新时间:2023-12-01 17:16:00 32 4
gpt4 key购买 nike

我只有在按照 React native - "this.setState is not a function" trying to animate background color? 的建议合并了 useEffect() Hook 后才遇到这个问题
有了以下内容,我得到

Rendered more hooks than during the previous render

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
新错误:

invalid prop 'color' supplied to 'Stylesheet'

Animated useNativeDriver was not specified

最佳答案

在您的第一次渲染中(我猜)只有 useFonts当您返回 <AppLoading /> 时将调用钩子(Hook)自从 !fontsLoaded .其余的钩子(Hook)在 else block ,这意味着您不会在每次渲染中拥有相同数量的 Hook 。
查看 https://reactjs.org/docs/hooks-rules.html更多解释,尤其是 https://reactjs.org/docs/hooks-rules.html#only-call-hooks-at-the-top-level

关于javascript - react 原生 - "Rendered more hooks than during the previous render?",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62824174/

32 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com