简体   繁体   English

如何根据 prop 有条件地渲染组件的颜色?

[英]How do I conditionally render the color of my component based on the prop?

The color of my component changes based on the value of the prop 'level'.我的组件的颜色会根据道具“级别”的值而变化。 When I tried using states to set the backgroundColor I realized that all the components have the same color as the state keeps changing for every comment.当我尝试使用状态来设置背景颜色时,我意识到所有组件的颜色都与 state 的颜色相同,因为每条评论都会不断变化。 I tried using references and states both to solve this however, I haven't been able to work out the problem as the code seems to work the same.我尝试使用引用和状态来解决这个问题,但是我无法解决问题,因为代码似乎工作相同。 Any help would be great, thanks.任何帮助都会很棒,谢谢。

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,
        }

You can use the second argument of useEffect to tell it when to run ( by default it runs on each update ).您可以使用useEffect的第二个参数来告诉它何时运行( 默认情况下它在每次更新时运行)。 I'd assume you want to pass level as your second argument based on your code, so that useEffect runs only when level is updated.我假设您想根据您的代码将level作为第二个参数传递,以便useEffect仅在level更新时运行。

The format for using useEffect with a second arg is like so:useEffect与第二个 arg 一起使用的格式如下:

         useEffect(() => {
              console.log(state); 
              //value of state is used here therefore must be passed as a dependency
         }, [state])

I would remove the state, useEffect, and ref, as they are overcomplicating the component.我将删除 state、useEffect 和 ref,因为它们使组件过于复杂。 Instead set the background color based on the value of level.而是根据 level 的值设置背景颜色。 The best way is similar to what you did in the useEffect but put it into a regular function instead.最好的方法类似于您在 useEffect 中所做的,但将其放入常规的 function 中。 Something like:就像是:

 const getBackgroundColor = () =>{ switch (level) { case 1: return colors.lightPurple case 2: return colors.crimson case 3: return colors.orange case 4: return colors.yellow } } const styles = (marginColor) => StyleSheet.create({ container:{ backgroundColor: getBackgroundColor(), }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何根据特定页面有条件地呈现 Gatsby 中的组件? - How do I conditionally render a component in Gatsby based on specific page? 如何将组件作为道具传递并有条件地渲染它? - How to pass component as a prop and render it conditionally? 如何在 React 中的对象数组上 map,然后根据先前的值有条件地渲染组件? - How do I map over an array of objects in React and then conditionally render a component based on a previous value? 如何根据本地存储中是否有数据有条件地渲染这个组件? - How can I conditionally render this component based on whether or not there is data in localstorage? 如何基于 state 渲染组件 - How do I render component based on state 我如何有条件地加载我的React组件? - How do i load my React component conditionally? ReactJS-如何从多个prop数组中渲染组件? - ReactJS - How do I render out a component from several prop arrays? React Reusable Button Component - 如何正确传递背景颜色道具? - React Reusable Button Component - How do I properly pass a background color prop? React:如何有效地制作我的组件,以便在道具更改时整个组件不会重新渲染? - React: How can I efficiently make my component so the entire component doesn't re-render when a prop changes? Vue:如何根据父数据渲染动态组件? - Vue: How do I render a dynamic component based on parent data?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM