skip to Main Content

With the following code, I want to change the text color whenever the material tab is switched. First of all, to explain the code I suggested, I am going to make a function implemented to change the table text color according to the range of each substance and operate it as an onToggle event in the return statement. How can I modify it while keeping the current logic as much as possible?

function DashBoard() {
    const [activeColor, setActiveColor] = useState("");
    
    // A function implemented to change the table text color according to the range of each substance
    const textColorChange = (e, value) => {
        let colorMapping = [
            { range: [15, 35, 75], colors: ["#2359C5", "#01B56E", "#F5C932", "#DA3539", "#666666"] },
            { range: [30, 80, 150], colors: ["#2359C5", "#01B56E", "#F5C932", "#DA3539", "#666666"] },
            { range: [0.03, 0.09, 0.15], colors: ["#2359C5", "#01B56E", "#F5C932", "#DA3539", "#666666"] },
            { range: [0.03, 0.06, 0.2], colors: ["#2359C5", "#01B56E", "#F5C932", "#DA3539", "#666666"] },
            { range: [0.02, 0.05, 0.15], colors: ["#2359C5", "#01B56E", "#F5C932", "#DA3539", "#666666"] },
            { range: [2, 9, 15], colors: ["#2359C5", "#01B56E", "#F5C932", "#DA3539", "#666666"] }
        ];
            
        switch(e.target.id) {
            case "tabPage0":
                for (let pm2Dot5 of colorMapping) {
                    value <= pm2Dot5[0].range[0] ? setActiveColor(pm2Dot5[0].colors[0])
                    : pm2Dot5[0].range[0] < value && value <= pm2Dot5[0].range[1] ? setActiveColor(pm2Dot5[0].colors[1])
                    : pm2Dot5[0].range[1] < value && value <= pm2Dot5[0].range[2] ? setActiveColor(pm2Dot5[0].colors[2])
                    : pm2Dot5[0].range[2] > value ? setActiveColor(pm2Dot5[0].colors[3])
                    : setActiveColor(pm2Dot5[0].colors[4]);
                };
                
                break;
            case "tabPage1":
                for (let pm10 of colorMapping) {
                    value <= pm10[1].range[0] ? setActiveColor(pm10[1].colors[0])
                    : pm10[1].range[0] < value && value <= pm10[1].range[1] ? setActiveColor(pm10[1].colors[1])
                    : pm10[1].range[1] < value && value <= pm10[1].range[2] ? setActiveColor(pm10[1].colors[2])
                    : pm10[1].range[2] > value ? setActiveColor(pm10[1].colors[3])
                    : setActiveColor(pm10[1].colors[4]);
                };
                
                break;
            case "tabPage2":
                for (let o3 of colorMapping) {
                    value <= o3[2].range[0] ? setActiveColor(o3[2].colors[0])
                    : o3[2].range[0] < value && value <= o3[2].range[1] ? setActiveColor(o3[2].colors[1])
                    : o3[2].range[1] < value && value <= o3[2].range[2] ? setActiveColor(o3[2].colors[2])
                    : o3[2].range[2] > value ? setActiveColor(o3[2].colors[3])
                    : setActiveColor(o3[2].colors[4]);
                };
                
                break;
            case "tabPage3":
                for (let no2 of colorMapping) {
                    value <= no2[3].range[0] ? setActiveColor(no2[3].colors[0])
                    : no2[3].range[0] < value && value <= no2[3].range[1] ? setActiveColor(no2[3].colors[1])
                    : no2[3].range[1] < value && value <= no2[3].range[2] ? setActiveColor(no2[3].colors[2])
                    : no2[3].range[2] > value ? setActiveColor(no2[3].colors[3])
                    : setActiveColor(no2[3].colors[4]);
                };
                
                break;
            case "tabPage4":
                for (let so2 of colorMapping) {
                    value <= so2[4].range[0] ? setActiveColor(so2[4].colors[0])
                    : so2[4].range[0] < value && value <= so2[4].range[1] ? setActiveColor(so2[4].colors[1])
                    : so2[4].range[1] < value && value <= so2[4].range[2] ? setActiveColor(so2[4].colors[2])
                    : so2[4].range[2] > value ? setActiveColor(so2[4].colors[3])
                    : setActiveColor(so2[4].colors[4]);
                };
                
                break;
            default:
                for (let co of colorMapping) {
                    value <= co[5].range[0] ? setActiveColor(co[5].colors[0])
                    : co[5].range[0] < value && value <= co[5].range[1] ? setActiveColor(co[5].colors[1])
                    : co[5].range[1] < value && value <= co[5].range[2] ? setActiveColor(co[5].colors[2])
                    : co[5].range[2] > value ? setActiveColor(co[5].colors[3])
                    : setActiveColor(co[5].colors[4]);
                };
                
                break;
        };
    };

    return (
        <Style.Table>
            <Style.Title className="caption-text" onToggle={tabHandler}>{airTitle}</Style.Title>
            <Style.StyledTable>

                <tbody>
                    {regionList.map((region, index) => (
                        <tr key={index}>
                            <td className="region">{region.sidoNm}</td>
                            <td className="number" onToggle={e => textColorChange(e, region.avgNow)} style={{ color: e => textColorChange(e, region.avgNow) }}>{region.avgNow}</td>
                            <td className="number" onToggle={e => textColorChange(e, region.avgTd)} style={{ color: e => textColorChange(e, region.avgTd) }}>{region.avgTd}</td>
                            <td className="number" onToggle={e => textColorChange(e, region.max)} style={{ color: e => textColorChange(e, region.max) }}>{region.max}</td>
                            <td className="number" onToggle={e => textColorChange(e, region.min)} style={{ color: e => textColorChange(e, region.min) }}>{region.min}</td>
                            <td className="number" onToggle={e => textColorChange(e, region.avgYd)} style={{ color: e => textColorChange(e, region.avgYd) }}>{region.avgYd}</td>
                        </tr>
                    ))}
                </tbody>
            </Style.StyledTable>
        </Style.Table>
    );
}

2

Answers


  1. Chosen as BEST ANSWER
    function DashBoard() {
    const tabNmList = [{text1: "PM2.5", text2: "㎍/㎥"}, {text1: "PM10", text2: "㎍/㎥"}, {text1: "O₃", text2: "ppm"},
     {text1: "NO₂", text2: "ppm"}, {text1: "SO₂", text2: "ppm"}, {text1: "CO", text2: "ppm"}];
    const [tabState, setTabState] = useState("tabPage0");
    
    const [pm2Dot5List, setPm2Dot5List] = useState([]);
    const [pm10List, setPm10List] = useState([]);
    const [o3List, setO3List] = useState([]);
    const [no2List, setNo2List] = useState([]);
    const [so2List, setSo2List] = useState([]);
    const [coList, setCoList] = useState([]);
    
    const [gridList, setGridList] = useState([]);
    
    const regionList = gridList.map(item => item);
    
    const [activeColor, setActiveColor] = useState("");
    
    const tabHandler = e => {
        setTabState(e.target.id);
        
        switch (e.target.id) {
            case "tabPage0":
                setIndexImg(pm2Dot5Index);
    
                var cities = [
                    { list: pm2Dot5List[1].avgNow, setColor: setSeoulColor },
                    { list: pm2Dot5List[2].avgNow, setColor: setBusanColor },
                    { list: pm2Dot5List[3].avgNow, setColor: setDaeguColor },
                    { list: pm2Dot5List[4].avgNow, setColor: setIncheonColor },
                    { list: pm2Dot5List[5].avgNow, setColor: setGwangjuColor },
                    { list: pm2Dot5List[6].avgNow, setColor: setDaejeonColor },
                    { list: pm2Dot5List[7].avgNow, setColor: setUlsanColor },
                    { list: pm2Dot5List[8].avgNow, setColor: setSejongColor },
                    { list: pm2Dot5List[9].avgNow, setColor: setGyeonggiColor },
                    { list: pm2Dot5List[10].avgNow, setColor: setGangwonColor },
                    { list: pm2Dot5List[11].avgNow, setColor: setChungbukColor },
                    { list: pm2Dot5List[12].avgNow, setColor: setChungnamColor },
                    { list: pm2Dot5List[13].avgNow, setColor: setJeonbukColor },
                    { list: pm2Dot5List[14].avgNow, setColor: setJeonnamColor },
                    { list: pm2Dot5List[15].avgNow, setColor: setGyeongbukColor },
                    { list: pm2Dot5List[16].avgNow, setColor: setGyeongnamColor },
                    { list: pm2Dot5List[17].avgNow, setColor: setJejuColor }
                ];
    
                for (let city of cities) {
                    city.list <= 15 ? city.setColor("#2359C5")
                    : city.list > 15 && city.list <= 35 ? city.setColor("#01B56E")
                    : city.list > 35 && city.list <= 75 ? city.setColor("#F5C932")
                    : city.list > 75 ? city.setColor("#DA3539")
                    : city.setColor("#666666");
                }
    
                setAirTitle("PM2.5 (㎍/㎥)");
    
                setGridList(pm2Dot5List);
    
                break;
            case "tabPage1":
                setIndexImg(pm10Index);
    
                var cities = [
                    { list: pm10List[1].avgNow, setColor: setSeoulColor },
                    { list: pm10List[2].avgNow, setColor: setBusanColor },
                    { list: pm10List[3].avgNow, setColor: setDaeguColor },
                    { list: pm10List[4].avgNow, setColor: setIncheonColor },
                    { list: pm10List[5].avgNow, setColor: setGwangjuColor },
                    { list: pm10List[6].avgNow, setColor: setDaejeonColor },
                    { list: pm10List[7].avgNow, setColor: setUlsanColor },
                    { list: pm10List[8].avgNow, setColor: setSejongColor },
                    { list: pm10List[9].avgNow, setColor: setGyeonggiColor },
                    { list: pm10List[10].avgNow, setColor: setGangwonColor },
                    { list: pm10List[11].avgNow, setColor: setChungbukColor },
                    { list: pm10List[12].avgNow, setColor: setChungnamColor },
                    { list: pm10List[13].avgNow, setColor: setJeonbukColor },
                    { list: pm10List[14].avgNow, setColor: setJeonnamColor },
                    { list: pm10List[15].avgNow, setColor: setGyeongbukColor },
                    { list: pm10List[16].avgNow, setColor: setGyeongnamColor },
                    { list: pm10List[17].avgNow, setColor: setJejuColor }
                ];
    
                for (let city of cities) {
                    city.list <= 30 ? city.setColor("#2359C5")
                    : city.list > 30 && city.list <= 80 ? city.setColor("#01B56E")
                    : city.list > 80 && city.list <= 150 ? city.setColor("#F5C932")
                    : city.list > 150 ? city.setColor("#DA3539")
                    : city.setColor("#666666");
                }
    
                setAirTitle("PM10 (㎍/㎥)");
    
                setGridList(pm10List);
    
                break;
            case "tabPage2":
                setIndexImg(o3Index);
    
                var cities = [
                    { list: o3List[1].avgNow, setColor: setSeoulColor },
                    { list: o3List[2].avgNow, setColor: setBusanColor },
                    { list: o3List[3].avgNow, setColor: setDaeguColor },
                    { list: o3List[4].avgNow, setColor: setIncheonColor },
                    { list: o3List[5].avgNow, setColor: setGwangjuColor },
                    { list: o3List[6].avgNow, setColor: setDaejeonColor },
                    { list: o3List[7].avgNow, setColor: setUlsanColor },
                    { list: o3List[8].avgNow, setColor: setSejongColor },
                    { list: o3List[9].avgNow, setColor: setGyeonggiColor },
                    { list: o3List[10].avgNow, setColor: setGangwonColor },
                    { list: o3List[11].avgNow, setColor: setChungbukColor },
                    { list: o3List[12].avgNow, setColor: setChungnamColor },
                    { list: o3List[13].avgNow, setColor: setJeonbukColor },
                    { list: o3List[14].avgNow, setColor: setJeonnamColor },
                    { list: o3List[15].avgNow, setColor: setGyeongbukColor },
                    { list: o3List[16].avgNow, setColor: setGyeongnamColor },
                    { list: o3List[17].avgNow, setColor: setJejuColor }
                ];
    
                for (let city of cities) {
                    city.list <= 0.03 ? city.setColor("#2359C5")
                    : city.list > 0.03 && city.list <= 0.09 ? city.setColor("#01B56E")
                    : city.list > 0.09 && city.list <= 0.15 ? city.setColor("#F5C932")
                    : city.list > 0.15 ? city.setColor("#DA3539")
                    : city.setColor("#666666");
                }
    
                setAirTitle("O₃ ppm");
    
                setGridList(o3List);
    
                break;
            case "tabPage3":
                setIndexImg(no2Index);
    
                var cities = [
                    { list: no2List[1].avgNow, setColor: setSeoulColor },
                    { list: no2List[2].avgNow, setColor: setBusanColor },
                    { list: no2List[3].avgNow, setColor: setDaeguColor },
                    { list: no2List[4].avgNow, setColor: setIncheonColor },
                    { list: no2List[5].avgNow, setColor: setGwangjuColor },
                    { list: no2List[6].avgNow, setColor: setDaejeonColor },
                    { list: no2List[7].avgNow, setColor: setUlsanColor },
                    { list: no2List[8].avgNow, setColor: setSejongColor },
                    { list: no2List[9].avgNow, setColor: setGyeonggiColor },
                    { list: no2List[10].avgNow, setColor: setGangwonColor },
                    { list: no2List[11].avgNow, setColor: setChungbukColor },
                    { list: no2List[12].avgNow, setColor: setChungnamColor },
                    { list: no2List[13].avgNow, setColor: setJeonbukColor },
                    { list: no2List[14].avgNow, setColor: setJeonnamColor },
                    { list: no2List[15].avgNow, setColor: setGyeongbukColor },
                    { list: no2List[16].avgNow, setColor: setGyeongnamColor },
                    { list: no2List[17].avgNow, setColor: setJejuColor }
                ];
    
                for (let city of cities) {
                    city.list <= 0.03 ? city.setColor("#2359C5")
                    : city.list > 0.03 && city.list <= 0.06 ? city.setColor("#01B56E")
                    : city.list > 0.06 && city.list <= 0.2 ? city.setColor("#F5C932")
                    : city.list > 0.2 ? city.setColor("#DA3539")
                    : city.setColor("#666666");
                }
    
                setAirTitle("NO₂ ppm");
    
                setGridList(no2List);
    
                break;
            case "tabPage4":
                setIndexImg(so2Index);
    
                var cities = [
                    { list: so2List[1].avgNow, setColor: setSeoulColor },
                    { list: so2List[2].avgNow, setColor: setBusanColor },
                    { list: so2List[3].avgNow, setColor: setDaeguColor },
                    { list: so2List[4].avgNow, setColor: setIncheonColor },
                    { list: so2List[5].avgNow, setColor: setGwangjuColor },
                    { list: so2List[6].avgNow, setColor: setDaejeonColor },
                    { list: so2List[7].avgNow, setColor: setUlsanColor },
                    { list: so2List[8].avgNow, setColor: setSejongColor },
                    { list: so2List[9].avgNow, setColor: setGyeonggiColor },
                    { list: so2List[10].avgNow, setColor: setGangwonColor },
                    { list: so2List[11].avgNow, setColor: setChungbukColor },
                    { list: so2List[12].avgNow, setColor: setChungnamColor },
                    { list: so2List[13].avgNow, setColor: setJeonbukColor },
                    { list: so2List[14].avgNow, setColor: setJeonnamColor },
                    { list: so2List[15].avgNow, setColor: setGyeongbukColor },
                    { list: so2List[16].avgNow, setColor: setGyeongnamColor },
                    { list: so2List[17].avgNow, setColor: setJejuColor }
                ];
    
                for (let city of cities) {
                    city.list <= 0.02 ? city.setColor("#2359C5")
                    : city.list > 0.02 && city.list <= 0.05 ? city.setColor("#01B56E")
                    : city.list > 0.05 && city.list <= 0.15 ? city.setColor("#F5C932")
                    : city.list > 0.15 ? city.setColor("#DA3539")
                    : city.setColor("#666666");
                }
    
                setAirTitle("SO₂ ppm");
    
                setGridList(so2List);
    
                break;
            default:
                setIndexImg(coIndex);
    
                var cities = [
                    { list: coList[1].avgNow, setColor: setSeoulColor },
                    { list: coList[2].avgNow, setColor: setBusanColor },
                    { list: coList[3].avgNow, setColor: setDaeguColor },
                    { list: coList[4].avgNow, setColor: setIncheonColor },
                    { list: coList[5].avgNow, setColor: setGwangjuColor },
                    { list: coList[6].avgNow, setColor: setDaejeonColor },
                    { list: coList[7].avgNow, setColor: setUlsanColor },
                    { list: coList[8].avgNow, setColor: setSejongColor },
                    { list: coList[9].avgNow, setColor: setGyeonggiColor },
                    { list: coList[10].avgNow, setColor: setGangwonColor },
                    { list: coList[11].avgNow, setColor: setChungbukColor },
                    { list: coList[12].avgNow, setColor: setChungnamColor },
                    { list: coList[13].avgNow, setColor: setJeonbukColor },
                    { list: coList[14].avgNow, setColor: setJeonnamColor },
                    { list: coList[15].avgNow, setColor: setGyeongbukColor },
                    { list: coList[16].avgNow, setColor: setGyeongnamColor },
                    { list: coList[17].avgNow, setColor: setJejuColor }
                ];
    
                for (let city of cities) {
                    city.list <= 2 ? city.setColor("#2359C5")
                    : city.list > 2 && city.list <= 9 ? city.setColor("#01B56E")
                    : city.list > 9 && city.list <= 15 ? city.setColor("#F5C932")
                    : city.list > 15 ? city.setColor("#DA3539")
                    : city.setColor("#666666");
                }
    
                setAirTitle("CO ppm");
    
                setGridList(coList);
    
                break;
        }
    };
    
    const textColorChange = (e, value) => {
        let colorMapping = [
            { range: [15, 35, 75], colors: ["#2359C5", "#01B56E", "#F5C932", "#DA3539", "#666666"] },
            { range: [30, 80, 150], colors: ["#2359C5", "#01B56E", "#F5C932", "#DA3539", "#666666"] },
            { range: [0.03, 0.09, 0.15], colors: ["#2359C5", "#01B56E", "#F5C932", "#DA3539", "#666666"] },
            { range: [0.03, 0.06, 0.2], colors: ["#2359C5", "#01B56E", "#F5C932", "#DA3539", "#666666"] },
            { range: [0.02, 0.05, 0.15], colors: ["#2359C5", "#01B56E", "#F5C932", "#DA3539", "#666666"] },
            { range: [2, 9, 15], colors: ["#2359C5", "#01B56E", "#F5C932", "#DA3539", "#666666"] }
        ];
    
        const tabIndex = parseInt(e.target.id.replace("tabPage", ""));
        const ranges = colorMapping[tabIndex].range;
        const colors = colorMapping[tabIndex].colors;
    
        for (let i = 0; i < ranges.length - 1; i++) {
                if (value <= ranges[i]) {
                        setActiveColor(colors[i]);
                        return;
                }
                if (value > ranges[i] && value <= ranges[i + 1]) {
                        setActiveColor(colors[i + 1]);
                        return;
                }
        }
        setActiveColor(colors[colors.length - 1]);
    };
    
    useEffect(() => {
        axios.post('/ais/srch/Dshdatas.do', {
            page: 'arpltn/pollMap',
            date: '2023070311'
        })
        .then(response => {
            setIsLoading(false);
    
            const pm2Dot5SeoulList = response.data.rstList6[1].avgNow;
            const pm2Dot5BusanList = response.data.rstList6[2].avgNow;
            const pm2Dot5DaeguList = response.data.rstList6[3].avgNow;
            const pm2Dot5IncheonList = response.data.rstList6[4].avgNow;
            const pm2Dot5GwangjuList = response.data.rstList6[5].avgNow;
            const pm2Dot5DaejeonList = response.data.rstList6[6].avgNow;
            const pm2Dot5UlsanList = response.data.rstList6[7].avgNow;
            const pm2Dot5SejongList = response.data.rstList6[8].avgNow;
            const pm2Dot5GyeonggiList = response.data.rstList6[9].avgNow;
            const pm2Dot5GangwonList = response.data.rstList6[10].avgNow;
            const pm2Dot5ChungbukList = response.data.rstList6[11].avgNow;
            const pm2Dot5ChungnamList = response.data.rstList6[12].avgNow;
            const pm2Dot5JeonbukList = response.data.rstList6[13].avgNow;
            const pm2Dot5JeonnamList = response.data.rstList6[14].avgNow;
            const pm2Dot5GyeongbukList = response.data.rstList6[15].avgNow;
            const pm2Dot5GyeongnamList = response.data.rstList6[16].avgNow;
            const pm2Dot5JejuList = response.data.rstList6[17].avgNow;
    
            var cities = [
                { list: pm2Dot5SeoulList, setColor: setSeoulColor },
                { list: pm2Dot5BusanList, setColor: setBusanColor },
                { list: pm2Dot5DaeguList, setColor: setDaeguColor },
                { list: pm2Dot5IncheonList, setColor: setIncheonColor },
                { list: pm2Dot5GwangjuList, setColor: setGwangjuColor },
                { list: pm2Dot5DaejeonList, setColor: setDaejeonColor },
                { list: pm2Dot5UlsanList, setColor: setUlsanColor },
                { list: pm2Dot5SejongList, setColor: setSejongColor },
                { list: pm2Dot5GyeonggiList, setColor: setGyeonggiColor },
                { list: pm2Dot5GangwonList, setColor: setGangwonColor },
                { list: pm2Dot5ChungbukList, setColor: setChungbukColor },
                { list: pm2Dot5ChungnamList, setColor: setChungnamColor },
                { list: pm2Dot5JeonbukList, setColor: setJeonbukColor },
                { list: pm2Dot5JeonnamList, setColor: setJeonnamColor },
                { list: pm2Dot5GyeongbukList, setColor: setGyeongbukColor },
                { list: pm2Dot5GyeongnamList, setColor: setGyeongnamColor },
                { list: pm2Dot5JejuList, setColor: setJejuColor }
            ];
    
            for (let city of cities) {
                city.list <= 15 ? city.setColor("#2359C5")
                : city.list > 15 && city.list <= 35 ? city.setColor("#01B56E")
                : city.list > 35 && city.list <= 75 ? city.setColor("#F5C932")
                : city.list > 75 ? city.setColor("#DA3539")
                : city.setColor("#666666");
            }
    
            // 변수에 담을 데이터를 서버에서 가져옴
            setPm2Dot5List(response.data.rstList6);
            setPm10List(response.data.rstList5);
            setO3List(response.data.rstList3);
            setNo2List(response.data.rstList2);
            setSo2List(response.data.rstList1);
            setCoList(response.data.rstList4);
            
            setGridList(response.data.rstList6);
        })
        .catch(e => {
            console.error(e);
        })
    
        setIndexImg(indexImg);
        setAirTitle(airTitle);
    },[]);
    
    useEffect(() => {
        let interval;
    
        if (isAutoClicked) {
            interval = setInterval(() => {
            const currentIndex = parseInt(tabState.replace("tabPage", ""));
            const nextIndex = (currentIndex + 1) % tabNmList.length;
            const nextPage = "tabPage" + nextIndex;
            setTabState(nextPage);
        
            switch (nextPage) {
                case "tabPage0":
                    setIndexImg(pm2Dot5Index);
    
                    var cities = [
                        { list: pm2Dot5List[1].avgNow, setColor: setSeoulColor },
                        { list: pm2Dot5List[2].avgNow, setColor: setBusanColor },
                        { list: pm2Dot5List[3].avgNow, setColor: setDaeguColor },
                        { list: pm2Dot5List[4].avgNow, setColor: setIncheonColor },
                        { list: pm2Dot5List[5].avgNow, setColor: setGwangjuColor },
                        { list: pm2Dot5List[6].avgNow, setColor: setDaejeonColor },
                        { list: pm2Dot5List[7].avgNow, setColor: setUlsanColor },
                        { list: pm2Dot5List[8].avgNow, setColor: setSejongColor },
                        { list: pm2Dot5List[9].avgNow, setColor: setGyeonggiColor },
                        { list: pm2Dot5List[10].avgNow, setColor: setGangwonColor },
                        { list: pm2Dot5List[11].avgNow, setColor: setChungbukColor },
                        { list: pm2Dot5List[12].avgNow, setColor: setChungnamColor },
                        { list: pm2Dot5List[13].avgNow, setColor: setJeonbukColor },
                        { list: pm2Dot5List[14].avgNow, setColor: setJeonnamColor },
                        { list: pm2Dot5List[15].avgNow, setColor: setGyeongbukColor },
                        { list: pm2Dot5List[16].avgNow, setColor: setGyeongnamColor },
                        { list: pm2Dot5List[17].avgNow, setColor: setJejuColor }
                    ];
    
                    for (let city of cities) {
                        city.list <= 15 ? city.setColor("#2359C5")
                        : city.list > 15 && city.list <= 35 ? city.setColor("#01B56E")
                        : city.list > 35 && city.list <= 75 ? city.setColor("#F5C932")
                        : city.list > 75 ? city.setColor("#DA3539")
                        : city.setColor("#666666");
                    }
    
                    setAirTitle("PM2.5 (㎍/㎥)");
    
                    setGridList(pm2Dot5List);
    
                    break;
                case "tabPage1":
                    setIndexImg(pm10Index);
    
                    var cities = [
                        { list: pm10List[1].avgNow, setColor: setSeoulColor },
                        { list: pm10List[2].avgNow, setColor: setBusanColor },
                        { list: pm10List[3].avgNow, setColor: setDaeguColor },
                        { list: pm10List[4].avgNow, setColor: setIncheonColor },
                        { list: pm10List[5].avgNow, setColor: setGwangjuColor },
                        { list: pm10List[6].avgNow, setColor: setDaejeonColor },
                        { list: pm10List[7].avgNow, setColor: setUlsanColor },
                        { list: pm10List[8].avgNow, setColor: setSejongColor },
                        { list: pm10List[9].avgNow, setColor: setGyeonggiColor },
                        { list: pm10List[10].avgNow, setColor: setGangwonColor },
                        { list: pm10List[11].avgNow, setColor: setChungbukColor },
                        { list: pm10List[12].avgNow, setColor: setChungnamColor },
                        { list: pm10List[13].avgNow, setColor: setJeonbukColor },
                        { list: pm10List[14].avgNow, setColor: setJeonnamColor },
                        { list: pm10List[15].avgNow, setColor: setGyeongbukColor },
                        { list: pm10List[16].avgNow, setColor: setGyeongnamColor },
                        { list: pm10List[17].avgNow, setColor: setJejuColor }
                    ];
    
                    for (let city of cities) {
                        city.list <= 30 ? city.setColor("#2359C5")
                        : city.list > 30 && city.list <= 80 ? city.setColor("#01B56E")
                        : city.list > 80 && city.list <= 150 ? city.setColor("#F5C932")
                        : city.list > 150 ? city.setColor("#DA3539")
                        : city.setColor("#666666");
                    }
    
                    setAirTitle("PM10 (㎍/㎥)");
    
                    setGridList(pm10List);
    
                    break;
                case "tabPage2":
                    setIndexImg(o3Index);
    
                    var cities = [
                        { list: o3List[1].avgNow, setColor: setSeoulColor },
                        { list: o3List[2].avgNow, setColor: setBusanColor },
                        { list: o3List[3].avgNow, setColor: setDaeguColor },
                        { list: o3List[4].avgNow, setColor: setIncheonColor },
                        { list: o3List[5].avgNow, setColor: setGwangjuColor },
                        { list: o3List[6].avgNow, setColor: setDaejeonColor },
                        { list: o3List[7].avgNow, setColor: setUlsanColor },
                        { list: o3List[8].avgNow, setColor: setSejongColor },
                        { list: o3List[9].avgNow, setColor: setGyeonggiColor },
                        { list: o3List[10].avgNow, setColor: setGangwonColor },
                        { list: o3List[11].avgNow, setColor: setChungbukColor },
                        { list: o3List[12].avgNow, setColor: setChungnamColor },
                        { list: o3List[13].avgNow, setColor: setJeonbukColor },
                        { list: o3List[14].avgNow, setColor: setJeonnamColor },
                        { list: o3List[15].avgNow, setColor: setGyeongbukColor },
                        { list: o3List[16].avgNow, setColor: setGyeongnamColor },
                        { list: o3List[17].avgNow, setColor: setJejuColor }
                    ];
    
                    for (let city of cities) {
                        city.list <= 0.03 ? city.setColor("#2359C5")
                        : city.list > 0.03 && city.list <= 0.09 ? city.setColor("#01B56E")
                        : city.list > 0.09 && city.list <= 0.15 ? city.setColor("#F5C932")
                        : city.list > 0.15 ? city.setColor("#DA3539")
                        : city.setColor("#666666");
                    }
    
                    setAirTitle("O₃ ppm");
    
                    setGridList(o3List);
    
                    break;
                case "tabPage3":
                    setIndexImg(no2Index);
    
                    var cities = [
                        { list: no2List[1].avgNow, setColor: setSeoulColor },
                        { list: no2List[2].avgNow, setColor: setBusanColor },
                        { list: no2List[3].avgNow, setColor: setDaeguColor },
                        { list: no2List[4].avgNow, setColor: setIncheonColor },
                        { list: no2List[5].avgNow, setColor: setGwangjuColor },
                        { list: no2List[6].avgNow, setColor: setDaejeonColor },
                        { list: no2List[7].avgNow, setColor: setUlsanColor },
                        { list: no2List[8].avgNow, setColor: setSejongColor },
                        { list: no2List[9].avgNow, setColor: setGyeonggiColor },
                        { list: no2List[10].avgNow, setColor: setGangwonColor },
                        { list: no2List[11].avgNow, setColor: setChungbukColor },
                        { list: no2List[12].avgNow, setColor: setChungnamColor },
                        { list: no2List[13].avgNow, setColor: setJeonbukColor },
                        { list: no2List[14].avgNow, setColor: setJeonnamColor },
                        { list: no2List[15].avgNow, setColor: setGyeongbukColor },
                        { list: no2List[16].avgNow, setColor: setGyeongnamColor },
                        { list: no2List[17].avgNow, setColor: setJejuColor }
                    ];
    
                    for (let city of cities) {
                        city.list <= 0.03 ? city.setColor("#2359C5")
                        : city.list > 0.03 && city.list <= 0.06 ? city.setColor("#01B56E")
                        : city.list > 0.06 && city.list <= 0.2 ? city.setColor("#F5C932")
                        : city.list > 0.2 ? city.setColor("#DA3539")
                        : city.setColor("#666666");
                    }
    
                    setAirTitle("NO₂ ppm");
    
                    setGridList(no2List);
    
                    break;
                case "tabPage4":
                    setIndexImg(so2Index);
    
                    var cities = [
                        { list: so2List[1].avgNow, setColor: setSeoulColor },
                        { list: so2List[2].avgNow, setColor: setBusanColor },
                        { list: so2List[3].avgNow, setColor: setDaeguColor },
                        { list: so2List[4].avgNow, setColor: setIncheonColor },
                        { list: so2List[5].avgNow, setColor: setGwangjuColor },
                        { list: so2List[6].avgNow, setColor: setDaejeonColor },
                        { list: so2List[7].avgNow, setColor: setUlsanColor },
                        { list: so2List[8].avgNow, setColor: setSejongColor },
                        { list: so2List[9].avgNow, setColor: setGyeonggiColor },
                        { list: so2List[10].avgNow, setColor: setGangwonColor },
                        { list: so2List[11].avgNow, setColor: setChungbukColor },
                        { list: so2List[12].avgNow, setColor: setChungnamColor },
                        { list: so2List[13].avgNow, setColor: setJeonbukColor },
                        { list: so2List[14].avgNow, setColor: setJeonnamColor },
                        { list: so2List[15].avgNow, setColor: setGyeongbukColor },
                        { list: so2List[16].avgNow, setColor: setGyeongnamColor },
                        { list: so2List[17].avgNow, setColor: setJejuColor }
                    ];
    
                    for (let city of cities) {
                        city.list <= 0.02 ? city.setColor("#2359C5")
                        : city.list > 0.02 && city.list <= 0.05 ? city.setColor("#01B56E")
                        : city.list > 0.05 && city.list <= 0.15 ? city.setColor("#F5C932")
                        : city.list > 0.15 ? city.setColor("#DA3539")
                        : city.setColor("#666666");
                    }
    
                    setAirTitle("SO₂ ppm");
    
                    setGridList(so2List);
    
                    break;
                default:
                    setIndexImg(coIndex);
    
                    var cities = [
                        { list: coList[1].avgNow, setColor: setSeoulColor },
                        { list: coList[2].avgNow, setColor: setBusanColor },
                        { list: coList[3].avgNow, setColor: setDaeguColor },
                        { list: coList[4].avgNow, setColor: setIncheonColor },
                        { list: coList[5].avgNow, setColor: setGwangjuColor },
                        { list: coList[6].avgNow, setColor: setDaejeonColor },
                        { list: coList[7].avgNow, setColor: setUlsanColor },
                        { list: coList[8].avgNow, setColor: setSejongColor },
                        { list: coList[9].avgNow, setColor: setGyeonggiColor },
                        { list: coList[10].avgNow, setColor: setGangwonColor },
                        { list: coList[11].avgNow, setColor: setChungbukColor },
                        { list: coList[12].avgNow, setColor: setChungnamColor },
                        { list: coList[13].avgNow, setColor: setJeonbukColor },
                        { list: coList[14].avgNow, setColor: setJeonnamColor },
                        { list: coList[15].avgNow, setColor: setGyeongbukColor },
                        { list: coList[16].avgNow, setColor: setGyeongnamColor },
                        { list: coList[17].avgNow, setColor: setJejuColor }
                    ];
    
                    for (let city of cities) {
                        city.list <= 2 ? city.setColor("#2359C5")
                        : city.list > 2 && city.list <= 9 ? city.setColor("#01B56E")
                        : city.list > 9 && city.list <= 15 ? city.setColor("#F5C932")
                        : city.list > 15 ? city.setColor("#DA3539")
                        : city.setColor("#666666");
                    }
    
                    setAirTitle("CO ppm");
    
                    setGridList(coList);
    
                    break;
                }
            }, 3000);
        };
        
        return () => {
            clearInterval(interval);
        };
    }, [tabState, isAutoClicked]);
    
    return (
        <Style.TabMatter>
            {tabNmList.map((param, index) => (
                <Style.BtnTab className={tabState === "tabPage" + index ? "btnTab active" : "btnTab"} id={"tabPage" + index}
                    onClick={tabHandler} key={index}>{param.text1}<br />{param.text2}
                </Style.BtnTab>
            ))}
        </Style.TabMatter>
        <Style.AirIndex>
            <img src={indexImg} alt={airTitle} />
        </Style.AirIndex>
        <Style.Table>
            <Style.Title className="caption-text" onToggle={tabHandler}>{airTitle}</Style.Title>
            <Style.StyledTable>
    
                <tbody>
                    {regionList.map((region, index) => (
                        <tr key={index}>
                            <td className="region">{region.sidoNm}</td>
                            <td className="number" onToggle={e => textColorChange(e, region.avgNow)} style={{ color: activeColor }}>{region.avgNow}</td>
                            <td className="number" onToggle={e => textColorChange(e, region.avgTd)} style={{ color: activeColor }}>{region.avgTd}</td>
                            <td className="number" onToggle={e => textColorChange(e, region.max)} style={{ color: activeColor }}>{region.max}</td>
                            <td className="number" onToggle={e => textColorChange(e, region.min)} style={{ color: activeColor }}>{region.min}</td>
                            <td className="number" onToggle={e => textColorChange(e, region.avgYd)} style={{ color: activeColor }}>{region.avgYd}</td>
                        </tr>
                    ))}
                </tbody>
            </Style.StyledTable>
        </Style.Table>
    );
    

    }


  2. It looks like you’re working with React and attempting to modify the text color based on certain conditions and the active tab. I’ll help you modify the code to achieve this while keeping the logic intact.

    However, it seems that there are some issues in your current code that need to be addressed. It looks like you’re using an array of objects, but your indexing and logic inside the switch statement are incorrect. Also, the range checks and comparisons are not accurate. I’ll provide you with a corrected version of the code:

    function DashBoard() {
        const [activeColor, setActiveColor] = useState("");
    
        const textColorChange = (e, value) => {
            let colorMapping = [
                { range: [15, 35, 75], colors: ["#2359C5", "#01B56E", "#F5C932", "#DA3539", "#666666"] },
                { range: [30, 80, 150], colors: ["#2359C5", "#01B56E", "#F5C932", "#DA3539", "#666666"] },
                { range: [0.03, 0.09, 0.15], colors: ["#2359C5", "#01B56E", "#F5C932", "#DA3539", "#666666"] },
                { range: [0.03, 0.06, 0.2], colors: ["#2359C5", "#01B56E", "#F5C932", "#DA3539", "#666666"] },
                { range: [0.02, 0.05, 0.15], colors: ["#2359C5", "#01B56E", "#F5C932", "#DA3539", "#666666"] },
                { range: [2, 9, 15], colors: ["#2359C5", "#01B56E", "#F5C932", "#DA3539", "#666666"] }
            ];
    
            const tabIndex = parseInt(e.target.id.replace("tabPage", ""));
            const ranges = colorMapping[tabIndex].range;
            const colors = colorMapping[tabIndex].colors;
    
            for (let i = 0; i < ranges.length - 1; i++) {
                if (value <= ranges[i]) {
                    setActiveColor(colors[i]);
                    return;
                }
                if (value > ranges[i] && value <= ranges[i + 1]) {
                    setActiveColor(colors[i + 1]);
                    return;
                }
            }
            setActiveColor(colors[colors.length - 1]);
        };
    
        return (
            <div>
                {/* Your tab components */}
                <button id="tabPage0" onClick={(e) => textColorChange(e, someValue)}>Tab 0</button>
                <button id="tabPage1" onClick={(e) => textColorChange(e, someValue)}>Tab 1</button>
                {/* ... Other tab buttons */}
                
                {/* Display content with text color */}
                <div style={{ color: activeColor }}>
                    Content with dynamic text color
                </div>
            </div>
        );
    }
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search