0

I am doing a small project with react lifecycles and I am attempting to change all box colors randomly every time the counter reaches a number divisible by 5 however, the boxes only change when the number is divisible by 10.

App.js:

import React from 'react';
import './App.css';
import Counter from "./components/Counter";
import Box from "./components/Box";

function App() {
    const randomColor = [
        "#0d0d8c",
        "#a41313",
        "#064826",
        "#996e00"
    ];

    const [number, setNumber] = React.useState(0);
    const [boxes, setBoxes] = React.useState([]);
    const [color, setColor] = React.useState('red');
    const [change, setChange] = React.useState(false);

    let box = boxes.map((obj, idx) =>
        <Box key={idx} color={color} />
    );

    let getColor = () => {
        if (boxes.length % 5 === 0) {
            let rand = Math.floor(Math.random() * randomColor.length);
            setColor(randomColor[rand]);
            return randomColor[rand];
        }
        return color;
    };

    React.useEffect(() => {
        if (boxes.length % 5 === 0) {
            let rand = Math.floor(Math.random() * randomColor.length);
            setColor(randomColor[rand]);
            return randomColor[rand];
        }
        return color;
    }, [color])

        React.useEffect(() => {
            if (number % 2 === 0) {
                let newBoxList = [...boxes];
                newBoxList.push({color: getColor()});
                setBoxes(newBoxList);
            }
        }, [number,change]);


        let reset = () => {
            setNumber(0);
            setBoxes([]);
            setChange(true);
        };

        return (
            <div className="App">
                <button onClick={() => setNumber(number + 1)}>Increase</button>
                <button onClick={reset}>reset</button>
                <Counter count={number}/>
                <div className="boxes">{box}</div>
            </div>
        );
    }

export default App;

deployed site: https://priceless-spence-9ae99a.netlify.app/

trinity
  • 35
  • 5

1 Answers1

0

I think the culprit is your useEffect() hook listening to number. Whenever number is even you are copying the existing contents of boxes and then adding new boxes to the existing boxes; you are not actually updating all boxes in the array. It seems your desire is to have all of those boxes, existing and new, be updated to a new color so you'll need to iterate through the array and update each.

I think it comes down to the fact that you are assigning the return value of getColor to your new boxes rather than the value of local state color.

qslabs
  • 406
  • 4
  • 8
  • Alternatively, you could move the tracking of color outside of `boxes` and instead reference it from local state whenever doing an in-line map of `boxes` in your return statement. That way every box is referencing the value in local state, you won't need to worry about updating each box! – qslabs Mar 02 '22 at 19:22