As you can see from the code, at first I want all the todos to be shown, then toggle between all, active and completed todos. I have made the arrays for the active and completed todos, but I’m not quite sure how to implement the switching between those lists. For example, how can I render only active todos instead of the all the todos, etc.?
return (
<div className='card'>
<TodoForm addTodo={addTodo} />
<div className='list-group'>
{
todos.map((item, index) =>
item.isEditing ? <TodoEditForm item={item} key={index} editTask={editTask} /> :
<Todo item={item} key={index}
toggleCompleted={toggleCompleted}
removeTodo={removeTodo}
toggleEdit={toggleEdit} />
)
}
{
bool ?
<div>
Active
</div>
:
<div>
Completed
</div>
}
</div>
<div className='toggle-todos-wrapper'>
<button className="toggle-todos-button btn btn-light">All</button>
<button className="toggle-todos-button btn btn-light">Active</button>
<button className="toggle-todos-button btn btn-light">Completed</button>
</div>
</div>
)
}
export default TodoWrapper
I tried using the ternary operator to switch between the active and completed todos, but the all todos remain.
2
Answers
As the code snippet is partial, I am not sure how are you exactly
making
the arrays foractive
andcompleted
todos. Therefore, I will assume that you have something like this in your code:Now either you are getting arrays from props or generating dynamically the following approach should work the same. To address the conditional rendering, you might need to change your existing code a bit. First of all instead of mapping over the
todos
array inside returnJSX
, move that logic to a re-usable function. Secondly, your ternary operator can work but currently you are using abool
variable, which I assume is a boolean variable, to determine the kind oftodos
to render. If you want to show 3 different kinds of Todos, then you shall change thebool
to local state. And might set its type to anenum
orconstant
array. But I will use strings for simplicity.The following changes shall get your code to conditionally toggle between the 3 types of Todos rendering:
I have removed the ternary logic from your existing code as it is being handled inside the newly created
renderTodos
function.You may proceed by introducing a new
state
variable that acts as a flag telling which category (all, completed or active) is being selected.That variable can therefore be used to filter the todos on the fly to display those in the category being selected or all of them when All button is selected.
Below a code snippet that demponstrates the aforementioned attempt:
And here’s a live demo on CodePen.
I hope you get the idea and I strongly advise you to customize it the way you see fit and build upon it based on your requirements. Hope you find that attempt helpful.
Finally, there might be some tweaks that can be applied as improvements and memorizing the filtering callback between re-renders is one of them especially when the
todos
set is getting larger. I recommend visiting the Docs to learn more about those potential tweaks.Please feel free to ask further questions about the suggested solution and I’ll gladly help you out.