0

I'm working on a basic Todo-app and currently implementing a progress bar to showcase how many of the added tasks have been completed.

The way I've done it is that I'm using the following method, which is called every time there is a change in my Task object. This is done with React useEffect and is working like a charm when it comes to the output of that console.log.

import {useEffect} from "react";

const Daily = ({ tasks, numberOfTasks, tasksCompleted }) => {
    // Updating progress with useEffect every time something happens with tasks
    useEffect(() => {
        updateProgressHandler();
    }, [tasks]);

    // Check and update progress based on completed tasks
    const updateProgressHandler = () => {
        numberOfTasks = tasks.length;
        tasksCompleted = tasks.filter(task => task.completed === true).length;
        console.log(`Updating progress for ${numberOfTasks} tasks, where ${tasksCompleted} are completed.`);
    }

    // Saving the completed % to a variable
    const completed = numberOfTasks / tasksCompleted * 10;
    //console.log(numberOfTasks);

    return (
        <div className="row">
                <div className="col">
                    <div className="progress progress-daily" style={{ height: "38px" }}>
                        <div
                            className="progress-bar progress-bar-striped progress-bar-animated bg-success"
                            role="progressbar"
                            aria-valuenow={tasksCompleted}
                            aria-valuemin="0"
                            aria-valuemax={numberOfTasks}
                            style={{width: `${completed}%`}}
                        >
                            {completed}%
                        </div>
                    </div>
                </div>
                <div className="col-1">
                    <button className="btn btn-success">Done</button>
                </div>
        </div>
    )
}

export default Daily;

My issue here however, is the fact that I can't figure out how to pass the numberOfTasks and tasksCompleted to my Daily progress object.

State is set within the App.js as follows:

function App() {
    // State
    const [numberOfTasks, tasksCompleted] = useState(0);
    const [inputText, setInputText] = useState("");
    const [tasks, setTasks] = useState([]);
    
    return ( ... );
}

So, while it works perfectly fine in the console.log, the value I'm getting in the progress bar itself is NaN. How come?

Pepelius
  • 1,576
  • 20
  • 34
  • You should use a state variable for this, rather than a normal one. https://stackoverflow.com/questions/54379896/what-is-state-in-react – ᴓᴓᴓ Jul 20 '21 at 22:34
  • Please include a [mcve]. – Emile Bergeron Jul 20 '21 at 22:35
  • @talfreds I am using state. In my App.js I'm applying useState as follows: `const [numberOfTasks, tasksCompleted] = useState(null);` But can't figure out how to utilize it outside the scope of this updateProgressHandler – Pepelius Jul 20 '21 at 22:45
  • You need to review the documentation on useState. In your `updateProgressHandler` function, you assign the value of tasksCompleted, thus why the console log works. That should be your setter function, it shouldn't contain values. Use the setter function to update the values. You also reassign "numberOfTasks " locally, that's why it doesn't work. You should use the setter. You can check this to start: https://stackoverflow.com/questions/53165945/what-is-usestate-in-react – ᴓᴓᴓ Jul 20 '21 at 22:48
  • @EmileBergeron I'm sorry but I'm not sure how I could possibly include what you've requested within a React app, where objects rely upon other objects etc to work. I'm not too keen to include all my js files to make a working example – Pepelius Jul 20 '21 at 22:50
  • Please _don't_ include all your JS files, but at least, include the minimal component and context so that we can understand where `tasks` is coming from, what's the scope of the variables, etc. Right now, we can only guess... – Emile Bergeron Jul 20 '21 at 22:53
  • 1
    Edited the question to have the full code of my Daily component, which acts as the progress bar. – Pepelius Jul 20 '21 at 22:56
  • 1
    Are you sure the line in App 'const [numberOfTasks, tasksCompleted] = useState(0);' is correct? Because it should be [numberOfTasks, setNumberOfTasks], I assume. Please check the question details. – Tarukami Jul 20 '21 at 23:49
  • I got it to work without using the state. So now it updates the values correctly, but a noob question here; it's not possible to save the state of the progress bar without saving it to the state, correct? I'm just thinking, since the calculations depend on the state of the tasks (completed tasks / number of tasks * 100), it should be good, right? – Pepelius Jul 21 '21 at 19:04

0 Answers0