Answer the question
In order to leave comments, you need to log in
How to select an element of an object when clicking on a button and display it?
There is an object:
const test = {
"title": {
"t1":{
"id": 1,
"title":"Test1",
"category": "Test1111"
},
"t2":{
"id": 2,
"title":"Test2",
"category": "Test2222"
},
"t3":{
"id": 3,
"title":"Test3",
"category": "Test3333"
}
}
}
{Object.keys(test.title).map(item => <div key={test.title[item].id}>{test.title[item].title} ---- {test.title[item].category}</div>)}
. Answer the question
In order to leave comments, you need to log in
const [ active, setActive ] = useState(null);
const item = test.title[active];
const onClick = e => setActive(e.target.dataset.id);
{Object.keys(test.title).map(n =>
<button key={n} data-id={n} onClick={onClick}>{n}</button>
)}
{item && <div>{item.title} ---- {item.category}</div>}
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question