【发布时间】:2022-01-10 03:37:27
【问题描述】:
我的组件的颜色会根据道具“级别”的值而变化。当我尝试使用状态设置背景颜色时,我意识到所有组件都具有相同的颜色,因为状态会随着每条评论的变化而不断变化。我尝试使用引用和状态来解决这个问题,但是我无法解决问题,因为代码似乎工作相同。任何帮助都会很棒,谢谢。
function CommentMargin({level}) {
const [marginColorState, setMarginColorState] = useState(colors.lightPurple);
const marginColor = useRef(null);
useEffect(() =>
{
switch (level) {
case 1:
setMarginColorState(colors.lightPurple);
marginColor(marginColorState);
case 2:
setMarginColorState(colors.crimson);
marginColor(marginColorState);
case 3:
setMarginColorState(colors.orange);
marginColor(marginColorState);
case 4:
setMarginColorState(colors.yellow);
marginColor(marginColorState);
}
}
)
return (
<View style={styles(marginColor).container}>
</View>
);
}
export default CommentMargin;
const styles = (marginColor) => StyleSheet.create({
container:{
backgroundColor: marginColor.current,
}
【问题讨论】:
标签: javascript css reactjs react-native styles