In my todo list, I need to display the details of each item when clicked on a button. There are two buttons available: "View Details" and "Hide Details". Below is the code snippet:
class Todos extends React.Component{
constructor(props){
super(props);
this.state={
shown:null,
todos: []
}
}
showDetails = (todo) => {
this.setState({
shown: todo.id
});
}
render(){
const { shown, todos } = this.state;
return(
<div>
<ul>
{todos.map((todo)=>(
<li key={todo.id}>
<span >{todo.title}</span>
<button onClick={()=>this.showDetails(todo)}>View Details</button>
{shown === todo.id && (<div>
Description: {todo.description}<br/>
Due Date: {todo.status} <br/>
</div>) }
</li>
))}
</ul>
</div>
)
}
}
The current issue is that clicking on any button shows the details for all todo items. How can I modify the code in order to only display details for the selected todo item?